1132718Skan/* C++ Parser.
2146895Skan   Copyright (C) 2000, 2001, 2002, 2003, 2004,
3169689Skan   2005  Free Software Foundation, Inc.
4132718Skan   Written by Mark Mitchell <mark@codesourcery.com>.
5132718Skan
6132718Skan   This file is part of GCC.
7132718Skan
8132718Skan   GCC is free software; you can redistribute it and/or modify it
9132718Skan   under the terms of the GNU General Public License as published by
10132718Skan   the Free Software Foundation; either version 2, or (at your option)
11132718Skan   any later version.
12132718Skan
13132718Skan   GCC is distributed in the hope that it will be useful, but
14132718Skan   WITHOUT ANY WARRANTY; without even the implied warranty of
15132718Skan   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16132718Skan   General Public License for more details.
17132718Skan
18132718Skan   You should have received a copy of the GNU General Public License
19132718Skan   along with GCC; see the file COPYING.  If not, write to the Free
20169689Skan   Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
21169689Skan   02110-1301, USA.  */
22132718Skan
23132718Skan#include "config.h"
24132718Skan#include "system.h"
25132718Skan#include "coretypes.h"
26132718Skan#include "tm.h"
27132718Skan#include "dyn-string.h"
28132718Skan#include "varray.h"
29132718Skan#include "cpplib.h"
30132718Skan#include "tree.h"
31132718Skan#include "cp-tree.h"
32132718Skan#include "c-pragma.h"
33132718Skan#include "decl.h"
34132718Skan#include "flags.h"
35132718Skan#include "diagnostic.h"
36132718Skan#include "toplev.h"
37132718Skan#include "output.h"
38169689Skan#include "target.h"
39169689Skan#include "cgraph.h"
40169689Skan#include "c-common.h"
41132718Skan
42132718Skan
43132718Skan/* The lexer.  */
44132718Skan
45169689Skan/* The cp_lexer_* routines mediate between the lexer proper (in libcpp
46169689Skan   and c-lex.c) and the C++ parser.  */
47132718Skan
48169689Skan/* A token's value and its associated deferred access checks and
49169689Skan   qualifying scope.  */
50132718Skan
51169689Skanstruct tree_check GTY(())
52169689Skan{
53169689Skan  /* The value associated with the token.  */
54169689Skan  tree value;
55169689Skan  /* The checks that have been associated with value.  */
56169689Skan  VEC (deferred_access_check, gc)* checks;
57169689Skan  /* The token's qualifying scope (used when it is a
58169689Skan     CPP_NESTED_NAME_SPECIFIER).  */
59169689Skan  tree qualifying_scope;
60169689Skan};
61132718Skan
62132718Skan/* A C++ token.  */
63132718Skan
64132718Skantypedef struct cp_token GTY (())
65132718Skan{
66132718Skan  /* The kind of token.  */
67132718Skan  ENUM_BITFIELD (cpp_ttype) type : 8;
68132718Skan  /* If this token is a keyword, this value indicates which keyword.
69132718Skan     Otherwise, this value is RID_MAX.  */
70132718Skan  ENUM_BITFIELD (rid) keyword : 8;
71132718Skan  /* Token flags.  */
72132718Skan  unsigned char flags;
73169689Skan  /* Identifier for the pragma.  */
74169689Skan  ENUM_BITFIELD (pragma_kind) pragma_kind : 6;
75169689Skan  /* True if this token is from a system header.  */
76169689Skan  BOOL_BITFIELD in_system_header : 1;
77169689Skan  /* True if this token is from a context where it is implicitly extern "C" */
78169689Skan  BOOL_BITFIELD implicit_extern_c : 1;
79169689Skan  /* True for a CPP_NAME token that is not a keyword (i.e., for which
80169689Skan     KEYWORD is RID_MAX) iff this name was looked up and found to be
81169689Skan     ambiguous.  An error has already been reported.  */
82169689Skan  BOOL_BITFIELD ambiguous_p : 1;
83169689Skan  /* The input file stack index at which this token was found.  */
84169689Skan  unsigned input_file_stack_index : INPUT_FILE_STACK_BITS;
85132718Skan  /* The value associated with this token, if any.  */
86169689Skan  union cp_token_value {
87169689Skan    /* Used for CPP_NESTED_NAME_SPECIFIER and CPP_TEMPLATE_ID.  */
88169689Skan    struct tree_check* GTY((tag ("1"))) tree_check_value;
89169689Skan    /* Use for all other tokens.  */
90169689Skan    tree GTY((tag ("0"))) value;
91169689Skan  } GTY((desc ("(%1.type == CPP_TEMPLATE_ID) || (%1.type == CPP_NESTED_NAME_SPECIFIER)"))) u;
92132718Skan  /* The location at which this token was found.  */
93132718Skan  location_t location;
94132718Skan} cp_token;
95132718Skan
96169689Skan/* We use a stack of token pointer for saving token sets.  */
97169689Skantypedef struct cp_token *cp_token_position;
98169689SkanDEF_VEC_P (cp_token_position);
99169689SkanDEF_VEC_ALLOC_P (cp_token_position,heap);
100132718Skan
101169689Skanstatic const cp_token eof_token =
102132718Skan{
103169689Skan  CPP_EOF, RID_MAX, 0, PRAGMA_NONE, 0, 0, false, 0, { NULL },
104169689Skan#if USE_MAPPED_LOCATION
105169689Skan  0
106169689Skan#else
107169689Skan  {0, 0}
108169689Skan#endif
109169689Skan};
110132718Skan
111132718Skan/* The cp_lexer structure represents the C++ lexer.  It is responsible
112132718Skan   for managing the token stream from the preprocessor and supplying
113169689Skan   it to the parser.  Tokens are never added to the cp_lexer after
114169689Skan   it is created.  */
115132718Skan
116132718Skantypedef struct cp_lexer GTY (())
117132718Skan{
118169689Skan  /* The memory allocated for the buffer.  NULL if this lexer does not
119169689Skan     own the token buffer.  */
120169689Skan  cp_token * GTY ((length ("%h.buffer_length"))) buffer;
121169689Skan  /* If the lexer owns the buffer, this is the number of tokens in the
122169689Skan     buffer.  */
123169689Skan  size_t buffer_length;
124169689Skan
125169689Skan  /* A pointer just past the last available token.  The tokens
126169689Skan     in this lexer are [buffer, last_token).  */
127169689Skan  cp_token_position GTY ((skip)) last_token;
128169689Skan
129169689Skan  /* The next available token.  If NEXT_TOKEN is &eof_token, then there are
130132718Skan     no more available tokens.  */
131169689Skan  cp_token_position GTY ((skip)) next_token;
132132718Skan
133132718Skan  /* A stack indicating positions at which cp_lexer_save_tokens was
134132718Skan     called.  The top entry is the most recent position at which we
135169689Skan     began saving tokens.  If the stack is non-empty, we are saving
136169689Skan     tokens.  */
137169689Skan  VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
138132718Skan
139169689Skan  /* The next lexer in a linked list of lexers.  */
140169689Skan  struct cp_lexer *next;
141132718Skan
142132718Skan  /* True if we should output debugging information.  */
143132718Skan  bool debugging_p;
144132718Skan
145169689Skan  /* True if we're in the context of parsing a pragma, and should not
146169689Skan     increment past the end-of-line marker.  */
147169689Skan  bool in_pragma;
148132718Skan} cp_lexer;
149132718Skan
150169689Skan/* cp_token_cache is a range of tokens.  There is no need to represent
151169689Skan   allocate heap memory for it, since tokens are never removed from the
152169689Skan   lexer's array.  There is also no need for the GC to walk through
153169689Skan   a cp_token_cache, since everything in here is referenced through
154169689Skan   a lexer.  */
155169689Skan
156169689Skantypedef struct cp_token_cache GTY(())
157169689Skan{
158169689Skan  /* The beginning of the token range.  */
159169689Skan  cp_token * GTY((skip)) first;
160169689Skan
161169689Skan  /* Points immediately after the last token in the range.  */
162169689Skan  cp_token * GTY ((skip)) last;
163169689Skan} cp_token_cache;
164169689Skan
165132718Skan/* Prototypes.  */
166132718Skan
167132718Skanstatic cp_lexer *cp_lexer_new_main
168132718Skan  (void);
169132718Skanstatic cp_lexer *cp_lexer_new_from_tokens
170169689Skan  (cp_token_cache *tokens);
171169689Skanstatic void cp_lexer_destroy
172169689Skan  (cp_lexer *);
173132718Skanstatic int cp_lexer_saving_tokens
174132718Skan  (const cp_lexer *);
175169689Skanstatic cp_token_position cp_lexer_token_position
176169689Skan  (cp_lexer *, bool);
177169689Skanstatic cp_token *cp_lexer_token_at
178169689Skan  (cp_lexer *, cp_token_position);
179132718Skanstatic void cp_lexer_get_preprocessor_token
180132718Skan  (cp_lexer *, cp_token *);
181169689Skanstatic inline cp_token *cp_lexer_peek_token
182132718Skan  (cp_lexer *);
183132718Skanstatic cp_token *cp_lexer_peek_nth_token
184132718Skan  (cp_lexer *, size_t);
185132718Skanstatic inline bool cp_lexer_next_token_is
186132718Skan  (cp_lexer *, enum cpp_ttype);
187132718Skanstatic bool cp_lexer_next_token_is_not
188132718Skan  (cp_lexer *, enum cpp_ttype);
189132718Skanstatic bool cp_lexer_next_token_is_keyword
190132718Skan  (cp_lexer *, enum rid);
191169689Skanstatic cp_token *cp_lexer_consume_token
192132718Skan  (cp_lexer *);
193132718Skanstatic void cp_lexer_purge_token
194132718Skan  (cp_lexer *);
195132718Skanstatic void cp_lexer_purge_tokens_after
196169689Skan  (cp_lexer *, cp_token_position);
197132718Skanstatic void cp_lexer_save_tokens
198132718Skan  (cp_lexer *);
199132718Skanstatic void cp_lexer_commit_tokens
200132718Skan  (cp_lexer *);
201132718Skanstatic void cp_lexer_rollback_tokens
202132718Skan  (cp_lexer *);
203169689Skan#ifdef ENABLE_CHECKING
204132718Skanstatic void cp_lexer_print_token
205132718Skan  (FILE *, cp_token *);
206169689Skanstatic inline bool cp_lexer_debugging_p
207132718Skan  (cp_lexer *);
208132718Skanstatic void cp_lexer_start_debugging
209132718Skan  (cp_lexer *) ATTRIBUTE_UNUSED;
210132718Skanstatic void cp_lexer_stop_debugging
211132718Skan  (cp_lexer *) ATTRIBUTE_UNUSED;
212169689Skan#else
213169689Skan/* If we define cp_lexer_debug_stream to NULL it will provoke warnings
214169689Skan   about passing NULL to functions that require non-NULL arguments
215169689Skan   (fputs, fprintf).  It will never be used, so all we need is a value
216169689Skan   of the right type that's guaranteed not to be NULL.  */
217169689Skan#define cp_lexer_debug_stream stdout
218169689Skan#define cp_lexer_print_token(str, tok) (void) 0
219169689Skan#define cp_lexer_debugging_p(lexer) 0
220169689Skan#endif /* ENABLE_CHECKING */
221132718Skan
222169689Skanstatic cp_token_cache *cp_token_cache_new
223169689Skan  (cp_token *, cp_token *);
224169689Skan
225169689Skanstatic void cp_parser_initial_pragma
226169689Skan  (cp_token *);
227169689Skan
228132718Skan/* Manifest constants.  */
229169689Skan#define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
230169689Skan#define CP_SAVED_TOKEN_STACK 5
231132718Skan
232132718Skan/* A token type for keywords, as opposed to ordinary identifiers.  */
233132718Skan#define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
234132718Skan
235132718Skan/* A token type for template-ids.  If a template-id is processed while
236132718Skan   parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
237132718Skan   the value of the CPP_TEMPLATE_ID is whatever was returned by
238132718Skan   cp_parser_template_id.  */
239132718Skan#define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
240132718Skan
241132718Skan/* A token type for nested-name-specifiers.  If a
242132718Skan   nested-name-specifier is processed while parsing tentatively, it is
243132718Skan   replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
244132718Skan   CPP_NESTED_NAME_SPECIFIER is whatever was returned by
245132718Skan   cp_parser_nested_name_specifier_opt.  */
246132718Skan#define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
247132718Skan
248132718Skan/* A token type for tokens that are not tokens at all; these are used
249169689Skan   to represent slots in the array where there used to be a token
250169689Skan   that has now been deleted.  */
251169689Skan#define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
252132718Skan
253169689Skan/* The number of token types, including C++-specific ones.  */
254169689Skan#define N_CP_TTYPES ((int) (CPP_PURGED + 1))
255169689Skan
256132718Skan/* Variables.  */
257132718Skan
258169689Skan#ifdef ENABLE_CHECKING
259132718Skan/* The stream to which debugging output should be written.  */
260132718Skanstatic FILE *cp_lexer_debug_stream;
261169689Skan#endif /* ENABLE_CHECKING */
262132718Skan
263132718Skan/* Create a new main C++ lexer, the lexer that gets tokens from the
264132718Skan   preprocessor.  */
265132718Skan
266132718Skanstatic cp_lexer *
267132718Skancp_lexer_new_main (void)
268132718Skan{
269169689Skan  cp_token first_token;
270132718Skan  cp_lexer *lexer;
271169689Skan  cp_token *pos;
272169689Skan  size_t alloc;
273169689Skan  size_t space;
274169689Skan  cp_token *buffer;
275132718Skan
276169689Skan  /* It's possible that parsing the first pragma will load a PCH file,
277169689Skan     which is a GC collection point.  So we have to do that before
278169689Skan     allocating any memory.  */
279169689Skan  cp_parser_initial_pragma (&first_token);
280169689Skan
281169689Skan  /* Tell c_lex_with_flags not to merge string constants.  */
282169689Skan  c_lex_return_raw_strings = true;
283169689Skan
284132718Skan  c_common_no_more_pch ();
285132718Skan
286132718Skan  /* Allocate the memory.  */
287169689Skan  lexer = GGC_CNEW (cp_lexer);
288132718Skan
289169689Skan#ifdef ENABLE_CHECKING
290169689Skan  /* Initially we are not debugging.  */
291169689Skan  lexer->debugging_p = false;
292169689Skan#endif /* ENABLE_CHECKING */
293169689Skan  lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
294169689Skan				   CP_SAVED_TOKEN_STACK);
295132718Skan
296169689Skan  /* Create the buffer.  */
297169689Skan  alloc = CP_LEXER_BUFFER_SIZE;
298169689Skan  buffer = GGC_NEWVEC (cp_token, alloc);
299132718Skan
300169689Skan  /* Put the first token in the buffer.  */
301169689Skan  space = alloc;
302169689Skan  pos = buffer;
303169689Skan  *pos = first_token;
304132718Skan
305169689Skan  /* Get the remaining tokens from the preprocessor.  */
306169689Skan  while (pos->type != CPP_EOF)
307169689Skan    {
308169689Skan      pos++;
309169689Skan      if (!--space)
310169689Skan	{
311169689Skan	  space = alloc;
312169689Skan	  alloc *= 2;
313169689Skan	  buffer = GGC_RESIZEVEC (cp_token, buffer, alloc);
314169689Skan	  pos = buffer + space;
315169689Skan	}
316169689Skan      cp_lexer_get_preprocessor_token (lexer, pos);
317169689Skan    }
318169689Skan  lexer->buffer = buffer;
319169689Skan  lexer->buffer_length = alloc - space;
320169689Skan  lexer->last_token = pos;
321169689Skan  lexer->next_token = lexer->buffer_length ? buffer : (cp_token *)&eof_token;
322132718Skan
323169689Skan  /* Subsequent preprocessor diagnostics should use compiler
324169689Skan     diagnostic functions to get the compiler source location.  */
325169689Skan  cpp_get_options (parse_in)->client_diagnostic = true;
326169689Skan  cpp_get_callbacks (parse_in)->error = cp_cpp_error;
327132718Skan
328169689Skan  gcc_assert (lexer->next_token->type != CPP_PURGED);
329132718Skan  return lexer;
330132718Skan}
331132718Skan
332169689Skan/* Create a new lexer whose token stream is primed with the tokens in
333169689Skan   CACHE.  When these tokens are exhausted, no new tokens will be read.  */
334132718Skan
335132718Skanstatic cp_lexer *
336169689Skancp_lexer_new_from_tokens (cp_token_cache *cache)
337132718Skan{
338169689Skan  cp_token *first = cache->first;
339169689Skan  cp_token *last = cache->last;
340169689Skan  cp_lexer *lexer = GGC_CNEW (cp_lexer);
341132718Skan
342169689Skan  /* We do not own the buffer.  */
343169689Skan  lexer->buffer = NULL;
344169689Skan  lexer->buffer_length = 0;
345169689Skan  lexer->next_token = first == last ? (cp_token *)&eof_token : first;
346169689Skan  lexer->last_token = last;
347132718Skan
348169689Skan  lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
349169689Skan				   CP_SAVED_TOKEN_STACK);
350132718Skan
351169689Skan#ifdef ENABLE_CHECKING
352169689Skan  /* Initially we are not debugging.  */
353132718Skan  lexer->debugging_p = false;
354169689Skan#endif
355132718Skan
356169689Skan  gcc_assert (lexer->next_token->type != CPP_PURGED);
357132718Skan  return lexer;
358132718Skan}
359132718Skan
360169689Skan/* Frees all resources associated with LEXER.  */
361169689Skan
362169689Skanstatic void
363169689Skancp_lexer_destroy (cp_lexer *lexer)
364169689Skan{
365169689Skan  if (lexer->buffer)
366169689Skan    ggc_free (lexer->buffer);
367169689Skan  VEC_free (cp_token_position, heap, lexer->saved_tokens);
368169689Skan  ggc_free (lexer);
369169689Skan}
370169689Skan
371132718Skan/* Returns nonzero if debugging information should be output.  */
372132718Skan
373169689Skan#ifdef ENABLE_CHECKING
374169689Skan
375132718Skanstatic inline bool
376132718Skancp_lexer_debugging_p (cp_lexer *lexer)
377132718Skan{
378132718Skan  return lexer->debugging_p;
379132718Skan}
380132718Skan
381169689Skan#endif /* ENABLE_CHECKING */
382132718Skan
383169689Skanstatic inline cp_token_position
384169689Skancp_lexer_token_position (cp_lexer *lexer, bool previous_p)
385132718Skan{
386169689Skan  gcc_assert (!previous_p || lexer->next_token != &eof_token);
387132718Skan
388169689Skan  return lexer->next_token - previous_p;
389132718Skan}
390132718Skan
391132718Skanstatic inline cp_token *
392169689Skancp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
393132718Skan{
394169689Skan  return pos;
395132718Skan}
396132718Skan
397132718Skan/* nonzero if we are presently saving tokens.  */
398132718Skan
399169689Skanstatic inline int
400132718Skancp_lexer_saving_tokens (const cp_lexer* lexer)
401132718Skan{
402169689Skan  return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
403132718Skan}
404132718Skan
405169689Skan/* Store the next token from the preprocessor in *TOKEN.  Return true
406169689Skan   if we reach EOF.  */
407132718Skan
408169689Skanstatic void
409169689Skancp_lexer_get_preprocessor_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
410169689Skan				 cp_token *token)
411132718Skan{
412169689Skan  static int is_extern_c = 0;
413132718Skan
414169689Skan   /* Get a new token from the preprocessor.  */
415169689Skan  token->type
416169689Skan    = c_lex_with_flags (&token->u.value, &token->location, &token->flags);
417169689Skan  token->input_file_stack_index = input_file_stack_tick;
418169689Skan  token->keyword = RID_MAX;
419169689Skan  token->pragma_kind = PRAGMA_NONE;
420169689Skan  token->in_system_header = in_system_header;
421132718Skan
422169689Skan  /* On some systems, some header files are surrounded by an
423169689Skan     implicit extern "C" block.  Set a flag in the token if it
424169689Skan     comes from such a header.  */
425169689Skan  is_extern_c += pending_lang_change;
426169689Skan  pending_lang_change = 0;
427169689Skan  token->implicit_extern_c = is_extern_c > 0;
428132718Skan
429169689Skan  /* Check to see if this token is a keyword.  */
430169689Skan  if (token->type == CPP_NAME)
431132718Skan    {
432169689Skan      if (C_IS_RESERVED_WORD (token->u.value))
433169689Skan	{
434169689Skan	  /* Mark this token as a keyword.  */
435169689Skan	  token->type = CPP_KEYWORD;
436169689Skan	  /* Record which keyword.  */
437169689Skan	  token->keyword = C_RID_CODE (token->u.value);
438169689Skan	  /* Update the value.  Some keywords are mapped to particular
439169689Skan	     entities, rather than simply having the value of the
440169689Skan	     corresponding IDENTIFIER_NODE.  For example, `__const' is
441169689Skan	     mapped to `const'.  */
442169689Skan	  token->u.value = ridpointers[token->keyword];
443169689Skan	}
444169689Skan      else
445169689Skan	{
446169689Skan	  token->ambiguous_p = false;
447169689Skan	  token->keyword = RID_MAX;
448169689Skan	}
449132718Skan    }
450169689Skan  /* Handle Objective-C++ keywords.  */
451169689Skan  else if (token->type == CPP_AT_NAME)
452132718Skan    {
453169689Skan      token->type = CPP_KEYWORD;
454169689Skan      switch (C_RID_CODE (token->u.value))
455132718Skan	{
456169689Skan	/* Map 'class' to '@class', 'private' to '@private', etc.  */
457169689Skan	case RID_CLASS: token->keyword = RID_AT_CLASS; break;
458169689Skan	case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
459169689Skan	case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
460169689Skan	case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
461169689Skan	case RID_THROW: token->keyword = RID_AT_THROW; break;
462169689Skan	case RID_TRY: token->keyword = RID_AT_TRY; break;
463169689Skan	case RID_CATCH: token->keyword = RID_AT_CATCH; break;
464169689Skan	default: token->keyword = C_RID_CODE (token->u.value);
465132718Skan	}
466132718Skan    }
467169689Skan  else if (token->type == CPP_PRAGMA)
468169689Skan    {
469169689Skan      /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST.  */
470169689Skan      token->pragma_kind = TREE_INT_CST_LOW (token->u.value);
471169689Skan      token->u.value = NULL_TREE;
472169689Skan    }
473132718Skan}
474132718Skan
475169689Skan/* Update the globals input_location and in_system_header and the
476169689Skan   input file stack from TOKEN.  */
477169689Skanstatic inline void
478169689Skancp_lexer_set_source_position_from_token (cp_token *token)
479132718Skan{
480169689Skan  if (token->type != CPP_EOF)
481132718Skan    {
482169689Skan      input_location = token->location;
483169689Skan      in_system_header = token->in_system_header;
484169689Skan      restore_input_file_stack (token->input_file_stack_index);
485132718Skan    }
486132718Skan}
487132718Skan
488132718Skan/* Return a pointer to the next token in the token stream, but do not
489132718Skan   consume it.  */
490132718Skan
491169689Skanstatic inline cp_token *
492169689Skancp_lexer_peek_token (cp_lexer *lexer)
493132718Skan{
494132718Skan  if (cp_lexer_debugging_p (lexer))
495132718Skan    {
496169689Skan      fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
497132718Skan      cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
498169689Skan      putc ('\n', cp_lexer_debug_stream);
499132718Skan    }
500169689Skan  return lexer->next_token;
501132718Skan}
502132718Skan
503132718Skan/* Return true if the next token has the indicated TYPE.  */
504132718Skan
505169689Skanstatic inline bool
506132718Skancp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
507132718Skan{
508169689Skan  return cp_lexer_peek_token (lexer)->type == type;
509132718Skan}
510132718Skan
511132718Skan/* Return true if the next token does not have the indicated TYPE.  */
512132718Skan
513169689Skanstatic inline bool
514132718Skancp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
515132718Skan{
516132718Skan  return !cp_lexer_next_token_is (lexer, type);
517132718Skan}
518132718Skan
519132718Skan/* Return true if the next token is the indicated KEYWORD.  */
520132718Skan
521169689Skanstatic inline bool
522132718Skancp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
523132718Skan{
524169689Skan  return cp_lexer_peek_token (lexer)->keyword == keyword;
525169689Skan}
526169689Skan
527169689Skan/* Return true if the next token is a keyword for a decl-specifier.  */
528169689Skan
529169689Skanstatic bool
530169689Skancp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
531169689Skan{
532132718Skan  cp_token *token;
533132718Skan
534132718Skan  token = cp_lexer_peek_token (lexer);
535169689Skan  switch (token->keyword)
536169689Skan    {
537169689Skan      /* Storage classes.  */
538169689Skan    case RID_AUTO:
539169689Skan    case RID_REGISTER:
540169689Skan    case RID_STATIC:
541169689Skan    case RID_EXTERN:
542169689Skan    case RID_MUTABLE:
543169689Skan    case RID_THREAD:
544169689Skan      /* Elaborated type specifiers.  */
545169689Skan    case RID_ENUM:
546169689Skan    case RID_CLASS:
547169689Skan    case RID_STRUCT:
548169689Skan    case RID_UNION:
549169689Skan    case RID_TYPENAME:
550169689Skan      /* Simple type specifiers.  */
551169689Skan    case RID_CHAR:
552169689Skan    case RID_WCHAR:
553169689Skan    case RID_BOOL:
554169689Skan    case RID_SHORT:
555169689Skan    case RID_INT:
556169689Skan    case RID_LONG:
557169689Skan    case RID_SIGNED:
558169689Skan    case RID_UNSIGNED:
559169689Skan    case RID_FLOAT:
560169689Skan    case RID_DOUBLE:
561169689Skan    case RID_VOID:
562169689Skan      /* GNU extensions.  */
563169689Skan    case RID_ATTRIBUTE:
564169689Skan    case RID_TYPEOF:
565169689Skan      return true;
566169689Skan
567169689Skan    default:
568169689Skan      return false;
569169689Skan    }
570132718Skan}
571132718Skan
572132718Skan/* Return a pointer to the Nth token in the token stream.  If N is 1,
573169689Skan   then this is precisely equivalent to cp_lexer_peek_token (except
574169689Skan   that it is not inline).  One would like to disallow that case, but
575169689Skan   there is one case (cp_parser_nth_token_starts_template_id) where
576169689Skan   the caller passes a variable for N and it might be 1.  */
577132718Skan
578132718Skanstatic cp_token *
579132718Skancp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
580132718Skan{
581132718Skan  cp_token *token;
582132718Skan
583132718Skan  /* N is 1-based, not zero-based.  */
584169689Skan  gcc_assert (n > 0);
585132718Skan
586169689Skan  if (cp_lexer_debugging_p (lexer))
587169689Skan    fprintf (cp_lexer_debug_stream,
588169689Skan	     "cp_lexer: peeking ahead %ld at token: ", (long)n);
589169689Skan
590169689Skan  --n;
591132718Skan  token = lexer->next_token;
592169689Skan  gcc_assert (!n || token != &eof_token);
593169689Skan  while (n != 0)
594132718Skan    {
595169689Skan      ++token;
596169689Skan      if (token == lexer->last_token)
597169689Skan	{
598169689Skan	  token = (cp_token *)&eof_token;
599169689Skan	  break;
600169689Skan	}
601169689Skan
602169689Skan      if (token->type != CPP_PURGED)
603169689Skan	--n;
604132718Skan    }
605132718Skan
606169689Skan  if (cp_lexer_debugging_p (lexer))
607132718Skan    {
608169689Skan      cp_lexer_print_token (cp_lexer_debug_stream, token);
609169689Skan      putc ('\n', cp_lexer_debug_stream);
610132718Skan    }
611132718Skan
612132718Skan  return token;
613132718Skan}
614132718Skan
615169689Skan/* Return the next token, and advance the lexer's next_token pointer
616169689Skan   to point to the next non-purged token.  */
617132718Skan
618132718Skanstatic cp_token *
619132718Skancp_lexer_consume_token (cp_lexer* lexer)
620132718Skan{
621169689Skan  cp_token *token = lexer->next_token;
622132718Skan
623169689Skan  gcc_assert (token != &eof_token);
624169689Skan  gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
625132718Skan
626169689Skan  do
627169689Skan    {
628169689Skan      lexer->next_token++;
629169689Skan      if (lexer->next_token == lexer->last_token)
630169689Skan	{
631169689Skan	  lexer->next_token = (cp_token *)&eof_token;
632169689Skan	  break;
633169689Skan	}
634132718Skan
635132718Skan    }
636169689Skan  while (lexer->next_token->type == CPP_PURGED);
637132718Skan
638169689Skan  cp_lexer_set_source_position_from_token (token);
639169689Skan
640132718Skan  /* Provide debugging output.  */
641132718Skan  if (cp_lexer_debugging_p (lexer))
642132718Skan    {
643169689Skan      fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
644132718Skan      cp_lexer_print_token (cp_lexer_debug_stream, token);
645169689Skan      putc ('\n', cp_lexer_debug_stream);
646132718Skan    }
647132718Skan
648132718Skan  return token;
649132718Skan}
650132718Skan
651169689Skan/* Permanently remove the next token from the token stream, and
652169689Skan   advance the next_token pointer to refer to the next non-purged
653169689Skan   token.  */
654132718Skan
655132718Skanstatic void
656132718Skancp_lexer_purge_token (cp_lexer *lexer)
657132718Skan{
658169689Skan  cp_token *tok = lexer->next_token;
659132718Skan
660169689Skan  gcc_assert (tok != &eof_token);
661169689Skan  tok->type = CPP_PURGED;
662169689Skan  tok->location = UNKNOWN_LOCATION;
663169689Skan  tok->u.value = NULL_TREE;
664169689Skan  tok->keyword = RID_MAX;
665169689Skan
666169689Skan  do
667132718Skan    {
668169689Skan      tok++;
669169689Skan      if (tok == lexer->last_token)
670169689Skan	{
671169689Skan	  tok = (cp_token *)&eof_token;
672169689Skan	  break;
673169689Skan	}
674132718Skan    }
675169689Skan  while (tok->type == CPP_PURGED);
676169689Skan  lexer->next_token = tok;
677132718Skan}
678132718Skan
679169689Skan/* Permanently remove all tokens after TOK, up to, but not
680132718Skan   including, the token that will be returned next by
681132718Skan   cp_lexer_peek_token.  */
682132718Skan
683132718Skanstatic void
684169689Skancp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
685132718Skan{
686169689Skan  cp_token *peek = lexer->next_token;
687132718Skan
688169689Skan  if (peek == &eof_token)
689169689Skan    peek = lexer->last_token;
690169689Skan
691169689Skan  gcc_assert (tok < peek);
692169689Skan
693169689Skan  for ( tok += 1; tok != peek; tok += 1)
694132718Skan    {
695169689Skan      tok->type = CPP_PURGED;
696169689Skan      tok->location = UNKNOWN_LOCATION;
697169689Skan      tok->u.value = NULL_TREE;
698169689Skan      tok->keyword = RID_MAX;
699132718Skan    }
700132718Skan}
701132718Skan
702132718Skan/* Begin saving tokens.  All tokens consumed after this point will be
703132718Skan   preserved.  */
704132718Skan
705132718Skanstatic void
706132718Skancp_lexer_save_tokens (cp_lexer* lexer)
707132718Skan{
708132718Skan  /* Provide debugging output.  */
709132718Skan  if (cp_lexer_debugging_p (lexer))
710132718Skan    fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
711132718Skan
712169689Skan  VEC_safe_push (cp_token_position, heap,
713169689Skan		 lexer->saved_tokens, lexer->next_token);
714132718Skan}
715132718Skan
716132718Skan/* Commit to the portion of the token stream most recently saved.  */
717132718Skan
718132718Skanstatic void
719132718Skancp_lexer_commit_tokens (cp_lexer* lexer)
720132718Skan{
721132718Skan  /* Provide debugging output.  */
722132718Skan  if (cp_lexer_debugging_p (lexer))
723132718Skan    fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
724132718Skan
725169689Skan  VEC_pop (cp_token_position, lexer->saved_tokens);
726132718Skan}
727132718Skan
728132718Skan/* Return all tokens saved since the last call to cp_lexer_save_tokens
729132718Skan   to the token stream.  Stop saving tokens.  */
730132718Skan
731132718Skanstatic void
732132718Skancp_lexer_rollback_tokens (cp_lexer* lexer)
733132718Skan{
734132718Skan  /* Provide debugging output.  */
735132718Skan  if (cp_lexer_debugging_p (lexer))
736132718Skan    fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
737132718Skan
738169689Skan  lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
739132718Skan}
740132718Skan
741132718Skan/* Print a representation of the TOKEN on the STREAM.  */
742132718Skan
743169689Skan#ifdef ENABLE_CHECKING
744169689Skan
745132718Skanstatic void
746169689Skancp_lexer_print_token (FILE * stream, cp_token *token)
747132718Skan{
748169689Skan  /* We don't use cpp_type2name here because the parser defines
749169689Skan     a few tokens of its own.  */
750169689Skan  static const char *const token_names[] = {
751169689Skan    /* cpplib-defined token types */
752169689Skan#define OP(e, s) #e,
753169689Skan#define TK(e, s) #e,
754169689Skan    TTYPE_TABLE
755169689Skan#undef OP
756169689Skan#undef TK
757169689Skan    /* C++ parser token types - see "Manifest constants", above.  */
758169689Skan    "KEYWORD",
759169689Skan    "TEMPLATE_ID",
760169689Skan    "NESTED_NAME_SPECIFIER",
761169689Skan    "PURGED"
762169689Skan  };
763132718Skan
764169689Skan  /* If we have a name for the token, print it out.  Otherwise, we
765169689Skan     simply give the numeric code.  */
766169689Skan  gcc_assert (token->type < ARRAY_SIZE(token_names));
767169689Skan  fputs (token_names[token->type], stream);
768169689Skan
769169689Skan  /* For some tokens, print the associated data.  */
770132718Skan  switch (token->type)
771132718Skan    {
772169689Skan    case CPP_KEYWORD:
773169689Skan      /* Some keywords have a value that is not an IDENTIFIER_NODE.
774169689Skan	 For example, `struct' is mapped to an INTEGER_CST.  */
775169689Skan      if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
776169689Skan	break;
777169689Skan      /* else fall through */
778132718Skan    case CPP_NAME:
779169689Skan      fputs (IDENTIFIER_POINTER (token->u.value), stream);
780132718Skan      break;
781132718Skan
782169689Skan    case CPP_STRING:
783169689Skan    case CPP_WSTRING:
784169689Skan      fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
785132718Skan      break;
786132718Skan
787132718Skan    default:
788132718Skan      break;
789132718Skan    }
790132718Skan}
791132718Skan
792132718Skan/* Start emitting debugging information.  */
793132718Skan
794132718Skanstatic void
795132718Skancp_lexer_start_debugging (cp_lexer* lexer)
796132718Skan{
797169689Skan  lexer->debugging_p = true;
798132718Skan}
799169689Skan
800132718Skan/* Stop emitting debugging information.  */
801132718Skan
802132718Skanstatic void
803132718Skancp_lexer_stop_debugging (cp_lexer* lexer)
804132718Skan{
805169689Skan  lexer->debugging_p = false;
806132718Skan}
807132718Skan
808169689Skan#endif /* ENABLE_CHECKING */
809169689Skan
810169689Skan/* Create a new cp_token_cache, representing a range of tokens.  */
811169689Skan
812169689Skanstatic cp_token_cache *
813169689Skancp_token_cache_new (cp_token *first, cp_token *last)
814169689Skan{
815169689Skan  cp_token_cache *cache = GGC_NEW (cp_token_cache);
816169689Skan  cache->first = first;
817169689Skan  cache->last = last;
818169689Skan  return cache;
819169689Skan}
820169689Skan
821132718Skan
822169689Skan/* Decl-specifiers.  */
823169689Skan
824169689Skan/* Set *DECL_SPECS to represent an empty decl-specifier-seq.  */
825169689Skan
826169689Skanstatic void
827169689Skanclear_decl_specs (cp_decl_specifier_seq *decl_specs)
828169689Skan{
829169689Skan  memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
830169689Skan}
831169689Skan
832169689Skan/* Declarators.  */
833169689Skan
834169689Skan/* Nothing other than the parser should be creating declarators;
835169689Skan   declarators are a semi-syntactic representation of C++ entities.
836169689Skan   Other parts of the front end that need to create entities (like
837169689Skan   VAR_DECLs or FUNCTION_DECLs) should do that directly.  */
838169689Skan
839169689Skanstatic cp_declarator *make_call_declarator
840169689Skan  (cp_declarator *, cp_parameter_declarator *, cp_cv_quals, tree);
841169689Skanstatic cp_declarator *make_array_declarator
842169689Skan  (cp_declarator *, tree);
843169689Skanstatic cp_declarator *make_pointer_declarator
844169689Skan  (cp_cv_quals, cp_declarator *);
845169689Skanstatic cp_declarator *make_reference_declarator
846169689Skan  (cp_cv_quals, cp_declarator *);
847169689Skanstatic cp_parameter_declarator *make_parameter_declarator
848169689Skan  (cp_decl_specifier_seq *, cp_declarator *, tree);
849169689Skanstatic cp_declarator *make_ptrmem_declarator
850169689Skan  (cp_cv_quals, tree, cp_declarator *);
851169689Skan
852169689Skan/* An erroneous declarator.  */
853169689Skanstatic cp_declarator *cp_error_declarator;
854169689Skan
855169689Skan/* The obstack on which declarators and related data structures are
856169689Skan   allocated.  */
857169689Skanstatic struct obstack declarator_obstack;
858169689Skan
859169689Skan/* Alloc BYTES from the declarator memory pool.  */
860169689Skan
861169689Skanstatic inline void *
862169689Skanalloc_declarator (size_t bytes)
863169689Skan{
864169689Skan  return obstack_alloc (&declarator_obstack, bytes);
865169689Skan}
866169689Skan
867169689Skan/* Allocate a declarator of the indicated KIND.  Clear fields that are
868169689Skan   common to all declarators.  */
869169689Skan
870169689Skanstatic cp_declarator *
871169689Skanmake_declarator (cp_declarator_kind kind)
872169689Skan{
873169689Skan  cp_declarator *declarator;
874169689Skan
875169689Skan  declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
876169689Skan  declarator->kind = kind;
877169689Skan  declarator->attributes = NULL_TREE;
878169689Skan  declarator->declarator = NULL;
879169689Skan
880169689Skan  return declarator;
881169689Skan}
882169689Skan
883169689Skan/* Make a declarator for a generalized identifier.  If
884169689Skan   QUALIFYING_SCOPE is non-NULL, the identifier is
885169689Skan   QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
886169689Skan   UNQUALIFIED_NAME.  SFK indicates the kind of special function this
887169689Skan   is, if any.   */
888169689Skan
889169689Skanstatic cp_declarator *
890169689Skanmake_id_declarator (tree qualifying_scope, tree unqualified_name,
891169689Skan		    special_function_kind sfk)
892169689Skan{
893169689Skan  cp_declarator *declarator;
894169689Skan
895169689Skan  /* It is valid to write:
896169689Skan
897169689Skan       class C { void f(); };
898169689Skan       typedef C D;
899169689Skan       void D::f();
900169689Skan
901169689Skan     The standard is not clear about whether `typedef const C D' is
902169689Skan     legal; as of 2002-09-15 the committee is considering that
903169689Skan     question.  EDG 3.0 allows that syntax.  Therefore, we do as
904169689Skan     well.  */
905169689Skan  if (qualifying_scope && TYPE_P (qualifying_scope))
906169689Skan    qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
907169689Skan
908169689Skan  gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
909169689Skan	      || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
910169689Skan	      || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
911169689Skan
912169689Skan  declarator = make_declarator (cdk_id);
913169689Skan  declarator->u.id.qualifying_scope = qualifying_scope;
914169689Skan  declarator->u.id.unqualified_name = unqualified_name;
915169689Skan  declarator->u.id.sfk = sfk;
916169689Skan
917169689Skan  return declarator;
918169689Skan}
919169689Skan
920169689Skan/* Make a declarator for a pointer to TARGET.  CV_QUALIFIERS is a list
921169689Skan   of modifiers such as const or volatile to apply to the pointer
922169689Skan   type, represented as identifiers.  */
923169689Skan
924169689Skancp_declarator *
925169689Skanmake_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
926169689Skan{
927169689Skan  cp_declarator *declarator;
928169689Skan
929169689Skan  declarator = make_declarator (cdk_pointer);
930169689Skan  declarator->declarator = target;
931169689Skan  declarator->u.pointer.qualifiers = cv_qualifiers;
932169689Skan  declarator->u.pointer.class_type = NULL_TREE;
933169689Skan
934169689Skan  return declarator;
935169689Skan}
936169689Skan
937169689Skan/* Like make_pointer_declarator -- but for references.  */
938169689Skan
939169689Skancp_declarator *
940169689Skanmake_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
941169689Skan{
942169689Skan  cp_declarator *declarator;
943169689Skan
944169689Skan  declarator = make_declarator (cdk_reference);
945169689Skan  declarator->declarator = target;
946169689Skan  declarator->u.pointer.qualifiers = cv_qualifiers;
947169689Skan  declarator->u.pointer.class_type = NULL_TREE;
948169689Skan
949169689Skan  return declarator;
950169689Skan}
951169689Skan
952169689Skan/* Like make_pointer_declarator -- but for a pointer to a non-static
953169689Skan   member of CLASS_TYPE.  */
954169689Skan
955169689Skancp_declarator *
956169689Skanmake_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
957169689Skan			cp_declarator *pointee)
958169689Skan{
959169689Skan  cp_declarator *declarator;
960169689Skan
961169689Skan  declarator = make_declarator (cdk_ptrmem);
962169689Skan  declarator->declarator = pointee;
963169689Skan  declarator->u.pointer.qualifiers = cv_qualifiers;
964169689Skan  declarator->u.pointer.class_type = class_type;
965169689Skan
966169689Skan  return declarator;
967169689Skan}
968169689Skan
969169689Skan/* Make a declarator for the function given by TARGET, with the
970169689Skan   indicated PARMS.  The CV_QUALIFIERS aply to the function, as in
971169689Skan   "const"-qualified member function.  The EXCEPTION_SPECIFICATION
972169689Skan   indicates what exceptions can be thrown.  */
973169689Skan
974169689Skancp_declarator *
975169689Skanmake_call_declarator (cp_declarator *target,
976169689Skan		      cp_parameter_declarator *parms,
977169689Skan		      cp_cv_quals cv_qualifiers,
978169689Skan		      tree exception_specification)
979169689Skan{
980169689Skan  cp_declarator *declarator;
981169689Skan
982169689Skan  declarator = make_declarator (cdk_function);
983169689Skan  declarator->declarator = target;
984169689Skan  declarator->u.function.parameters = parms;
985169689Skan  declarator->u.function.qualifiers = cv_qualifiers;
986169689Skan  declarator->u.function.exception_specification = exception_specification;
987169689Skan
988169689Skan  return declarator;
989169689Skan}
990169689Skan
991169689Skan/* Make a declarator for an array of BOUNDS elements, each of which is
992169689Skan   defined by ELEMENT.  */
993169689Skan
994169689Skancp_declarator *
995169689Skanmake_array_declarator (cp_declarator *element, tree bounds)
996169689Skan{
997169689Skan  cp_declarator *declarator;
998169689Skan
999169689Skan  declarator = make_declarator (cdk_array);
1000169689Skan  declarator->declarator = element;
1001169689Skan  declarator->u.array.bounds = bounds;
1002169689Skan
1003169689Skan  return declarator;
1004169689Skan}
1005169689Skan
1006169689Skancp_parameter_declarator *no_parameters;
1007169689Skan
1008169689Skan/* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1009169689Skan   DECLARATOR and DEFAULT_ARGUMENT.  */
1010169689Skan
1011169689Skancp_parameter_declarator *
1012169689Skanmake_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1013169689Skan			   cp_declarator *declarator,
1014169689Skan			   tree default_argument)
1015169689Skan{
1016169689Skan  cp_parameter_declarator *parameter;
1017169689Skan
1018169689Skan  parameter = ((cp_parameter_declarator *)
1019169689Skan	       alloc_declarator (sizeof (cp_parameter_declarator)));
1020169689Skan  parameter->next = NULL;
1021169689Skan  if (decl_specifiers)
1022169689Skan    parameter->decl_specifiers = *decl_specifiers;
1023169689Skan  else
1024169689Skan    clear_decl_specs (&parameter->decl_specifiers);
1025169689Skan  parameter->declarator = declarator;
1026169689Skan  parameter->default_argument = default_argument;
1027169689Skan  parameter->ellipsis_p = false;
1028169689Skan
1029169689Skan  return parameter;
1030169689Skan}
1031169689Skan
1032169689Skan/* Returns true iff DECLARATOR  is a declaration for a function.  */
1033169689Skan
1034169689Skanstatic bool
1035169689Skanfunction_declarator_p (const cp_declarator *declarator)
1036169689Skan{
1037169689Skan  while (declarator)
1038169689Skan    {
1039169689Skan      if (declarator->kind == cdk_function
1040169689Skan	  && declarator->declarator->kind == cdk_id)
1041169689Skan	return true;
1042169689Skan      if (declarator->kind == cdk_id
1043169689Skan	  || declarator->kind == cdk_error)
1044169689Skan	return false;
1045169689Skan      declarator = declarator->declarator;
1046169689Skan    }
1047169689Skan  return false;
1048169689Skan}
1049169689Skan
1050132718Skan/* The parser.  */
1051132718Skan
1052132718Skan/* Overview
1053132718Skan   --------
1054132718Skan
1055132718Skan   A cp_parser parses the token stream as specified by the C++
1056132718Skan   grammar.  Its job is purely parsing, not semantic analysis.  For
1057132718Skan   example, the parser breaks the token stream into declarators,
1058132718Skan   expressions, statements, and other similar syntactic constructs.
1059132718Skan   It does not check that the types of the expressions on either side
1060132718Skan   of an assignment-statement are compatible, or that a function is
1061132718Skan   not declared with a parameter of type `void'.
1062132718Skan
1063132718Skan   The parser invokes routines elsewhere in the compiler to perform
1064132718Skan   semantic analysis and to build up the abstract syntax tree for the
1065169689Skan   code processed.
1066132718Skan
1067132718Skan   The parser (and the template instantiation code, which is, in a
1068132718Skan   way, a close relative of parsing) are the only parts of the
1069132718Skan   compiler that should be calling push_scope and pop_scope, or
1070132718Skan   related functions.  The parser (and template instantiation code)
1071132718Skan   keeps track of what scope is presently active; everything else
1072132718Skan   should simply honor that.  (The code that generates static
1073132718Skan   initializers may also need to set the scope, in order to check
1074132718Skan   access control correctly when emitting the initializers.)
1075132718Skan
1076132718Skan   Methodology
1077132718Skan   -----------
1078169689Skan
1079132718Skan   The parser is of the standard recursive-descent variety.  Upcoming
1080132718Skan   tokens in the token stream are examined in order to determine which
1081132718Skan   production to use when parsing a non-terminal.  Some C++ constructs
1082132718Skan   require arbitrary look ahead to disambiguate.  For example, it is
1083132718Skan   impossible, in the general case, to tell whether a statement is an
1084132718Skan   expression or declaration without scanning the entire statement.
1085132718Skan   Therefore, the parser is capable of "parsing tentatively."  When the
1086132718Skan   parser is not sure what construct comes next, it enters this mode.
1087132718Skan   Then, while we attempt to parse the construct, the parser queues up
1088132718Skan   error messages, rather than issuing them immediately, and saves the
1089132718Skan   tokens it consumes.  If the construct is parsed successfully, the
1090132718Skan   parser "commits", i.e., it issues any queued error messages and
1091132718Skan   the tokens that were being preserved are permanently discarded.
1092132718Skan   If, however, the construct is not parsed successfully, the parser
1093132718Skan   rolls back its state completely so that it can resume parsing using
1094132718Skan   a different alternative.
1095132718Skan
1096132718Skan   Future Improvements
1097132718Skan   -------------------
1098132718Skan
1099169689Skan   The performance of the parser could probably be improved substantially.
1100169689Skan   We could often eliminate the need to parse tentatively by looking ahead
1101169689Skan   a little bit.  In some places, this approach might not entirely eliminate
1102169689Skan   the need to parse tentatively, but it might still speed up the average
1103169689Skan   case.  */
1104132718Skan
1105132718Skan/* Flags that are passed to some parsing functions.  These values can
1106132718Skan   be bitwise-ored together.  */
1107132718Skan
1108132718Skantypedef enum cp_parser_flags
1109132718Skan{
1110132718Skan  /* No flags.  */
1111132718Skan  CP_PARSER_FLAGS_NONE = 0x0,
1112132718Skan  /* The construct is optional.  If it is not present, then no error
1113132718Skan     should be issued.  */
1114132718Skan  CP_PARSER_FLAGS_OPTIONAL = 0x1,
1115132718Skan  /* When parsing a type-specifier, do not allow user-defined types.  */
1116132718Skan  CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1117132718Skan} cp_parser_flags;
1118132718Skan
1119132718Skan/* The different kinds of declarators we want to parse.  */
1120132718Skan
1121132718Skantypedef enum cp_parser_declarator_kind
1122132718Skan{
1123169689Skan  /* We want an abstract declarator.  */
1124132718Skan  CP_PARSER_DECLARATOR_ABSTRACT,
1125132718Skan  /* We want a named declarator.  */
1126132718Skan  CP_PARSER_DECLARATOR_NAMED,
1127132718Skan  /* We don't mind, but the name must be an unqualified-id.  */
1128132718Skan  CP_PARSER_DECLARATOR_EITHER
1129132718Skan} cp_parser_declarator_kind;
1130132718Skan
1131169689Skan/* The precedence values used to parse binary expressions.  The minimum value
1132169689Skan   of PREC must be 1, because zero is reserved to quickly discriminate
1133169689Skan   binary operators from other tokens.  */
1134132718Skan
1135169689Skanenum cp_parser_prec
1136132718Skan{
1137169689Skan  PREC_NOT_OPERATOR,
1138169689Skan  PREC_LOGICAL_OR_EXPRESSION,
1139169689Skan  PREC_LOGICAL_AND_EXPRESSION,
1140169689Skan  PREC_INCLUSIVE_OR_EXPRESSION,
1141169689Skan  PREC_EXCLUSIVE_OR_EXPRESSION,
1142169689Skan  PREC_AND_EXPRESSION,
1143169689Skan  PREC_EQUALITY_EXPRESSION,
1144169689Skan  PREC_RELATIONAL_EXPRESSION,
1145169689Skan  PREC_SHIFT_EXPRESSION,
1146169689Skan  PREC_ADDITIVE_EXPRESSION,
1147169689Skan  PREC_MULTIPLICATIVE_EXPRESSION,
1148169689Skan  PREC_PM_EXPRESSION,
1149169689Skan  NUM_PREC_VALUES = PREC_PM_EXPRESSION
1150169689Skan};
1151169689Skan
1152169689Skan/* A mapping from a token type to a corresponding tree node type, with a
1153169689Skan   precedence value.  */
1154169689Skan
1155169689Skantypedef struct cp_parser_binary_operations_map_node
1156169689Skan{
1157132718Skan  /* The token type.  */
1158169689Skan  enum cpp_ttype token_type;
1159132718Skan  /* The corresponding tree code.  */
1160169689Skan  enum tree_code tree_type;
1161169689Skan  /* The precedence of this operator.  */
1162169689Skan  enum cp_parser_prec prec;
1163169689Skan} cp_parser_binary_operations_map_node;
1164132718Skan
1165132718Skan/* The status of a tentative parse.  */
1166132718Skan
1167132718Skantypedef enum cp_parser_status_kind
1168132718Skan{
1169132718Skan  /* No errors have occurred.  */
1170132718Skan  CP_PARSER_STATUS_KIND_NO_ERROR,
1171132718Skan  /* An error has occurred.  */
1172132718Skan  CP_PARSER_STATUS_KIND_ERROR,
1173132718Skan  /* We are committed to this tentative parse, whether or not an error
1174132718Skan     has occurred.  */
1175132718Skan  CP_PARSER_STATUS_KIND_COMMITTED
1176132718Skan} cp_parser_status_kind;
1177132718Skan
1178169689Skantypedef struct cp_parser_expression_stack_entry
1179169689Skan{
1180169689Skan  tree lhs;
1181169689Skan  enum tree_code tree_type;
1182169689Skan  int prec;
1183169689Skan} cp_parser_expression_stack_entry;
1184169689Skan
1185169689Skan/* The stack for storing partial expressions.  We only need NUM_PREC_VALUES
1186169689Skan   entries because precedence levels on the stack are monotonically
1187169689Skan   increasing.  */
1188169689Skantypedef struct cp_parser_expression_stack_entry
1189169689Skan  cp_parser_expression_stack[NUM_PREC_VALUES];
1190169689Skan
1191132718Skan/* Context that is saved and restored when parsing tentatively.  */
1192132718Skantypedef struct cp_parser_context GTY (())
1193132718Skan{
1194132718Skan  /* If this is a tentative parsing context, the status of the
1195132718Skan     tentative parse.  */
1196132718Skan  enum cp_parser_status_kind status;
1197132718Skan  /* If non-NULL, we have just seen a `x->' or `x.' expression.  Names
1198132718Skan     that are looked up in this context must be looked up both in the
1199132718Skan     scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1200132718Skan     the context of the containing expression.  */
1201132718Skan  tree object_type;
1202169689Skan
1203132718Skan  /* The next parsing context in the stack.  */
1204132718Skan  struct cp_parser_context *next;
1205132718Skan} cp_parser_context;
1206132718Skan
1207132718Skan/* Prototypes.  */
1208132718Skan
1209132718Skan/* Constructors and destructors.  */
1210132718Skan
1211132718Skanstatic cp_parser_context *cp_parser_context_new
1212132718Skan  (cp_parser_context *);
1213132718Skan
1214132718Skan/* Class variables.  */
1215132718Skan
1216169689Skanstatic GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1217132718Skan
1218169689Skan/* The operator-precedence table used by cp_parser_binary_expression.
1219169689Skan   Transformed into an associative array (binops_by_token) by
1220169689Skan   cp_parser_new.  */
1221169689Skan
1222169689Skanstatic const cp_parser_binary_operations_map_node binops[] = {
1223169689Skan  { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1224169689Skan  { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1225169689Skan
1226169689Skan  { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1227169689Skan  { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1228169689Skan  { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1229169689Skan
1230169689Skan  { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1231169689Skan  { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1232169689Skan
1233169689Skan  { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1234169689Skan  { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1235169689Skan
1236169689Skan  { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1237169689Skan  { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1238169689Skan  { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1239169689Skan  { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1240169689Skan
1241169689Skan  { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1242169689Skan  { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1243169689Skan
1244169689Skan  { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1245169689Skan
1246169689Skan  { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1247169689Skan
1248169689Skan  { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1249169689Skan
1250169689Skan  { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1251169689Skan
1252169689Skan  { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1253169689Skan};
1254169689Skan
1255169689Skan/* The same as binops, but initialized by cp_parser_new so that
1256169689Skan   binops_by_token[N].token_type == N.  Used in cp_parser_binary_expression
1257169689Skan   for speed.  */
1258169689Skanstatic cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1259169689Skan
1260132718Skan/* Constructors and destructors.  */
1261132718Skan
1262132718Skan/* Construct a new context.  The context below this one on the stack
1263132718Skan   is given by NEXT.  */
1264132718Skan
1265132718Skanstatic cp_parser_context *
1266132718Skancp_parser_context_new (cp_parser_context* next)
1267132718Skan{
1268132718Skan  cp_parser_context *context;
1269132718Skan
1270132718Skan  /* Allocate the storage.  */
1271132718Skan  if (cp_parser_context_free_list != NULL)
1272132718Skan    {
1273132718Skan      /* Pull the first entry from the free list.  */
1274132718Skan      context = cp_parser_context_free_list;
1275132718Skan      cp_parser_context_free_list = context->next;
1276132718Skan      memset (context, 0, sizeof (*context));
1277132718Skan    }
1278132718Skan  else
1279169689Skan    context = GGC_CNEW (cp_parser_context);
1280169689Skan
1281132718Skan  /* No errors have occurred yet in this context.  */
1282132718Skan  context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1283132718Skan  /* If this is not the bottomost context, copy information that we
1284132718Skan     need from the previous context.  */
1285132718Skan  if (next)
1286132718Skan    {
1287132718Skan      /* If, in the NEXT context, we are parsing an `x->' or `x.'
1288132718Skan	 expression, then we are parsing one in this context, too.  */
1289132718Skan      context->object_type = next->object_type;
1290132718Skan      /* Thread the stack.  */
1291132718Skan      context->next = next;
1292132718Skan    }
1293132718Skan
1294132718Skan  return context;
1295132718Skan}
1296132718Skan
1297132718Skan/* The cp_parser structure represents the C++ parser.  */
1298132718Skan
1299132718Skantypedef struct cp_parser GTY(())
1300132718Skan{
1301132718Skan  /* The lexer from which we are obtaining tokens.  */
1302132718Skan  cp_lexer *lexer;
1303132718Skan
1304132718Skan  /* The scope in which names should be looked up.  If NULL_TREE, then
1305132718Skan     we look up names in the scope that is currently open in the
1306132718Skan     source program.  If non-NULL, this is either a TYPE or
1307169689Skan     NAMESPACE_DECL for the scope in which we should look.  It can
1308169689Skan     also be ERROR_MARK, when we've parsed a bogus scope.
1309132718Skan
1310132718Skan     This value is not cleared automatically after a name is looked
1311132718Skan     up, so we must be careful to clear it before starting a new look
1312132718Skan     up sequence.  (If it is not cleared, then `X::Y' followed by `Z'
1313132718Skan     will look up `Z' in the scope of `X', rather than the current
1314132718Skan     scope.)  Unfortunately, it is difficult to tell when name lookup
1315132718Skan     is complete, because we sometimes peek at a token, look it up,
1316169689Skan     and then decide not to consume it.   */
1317132718Skan  tree scope;
1318132718Skan
1319132718Skan  /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1320132718Skan     last lookup took place.  OBJECT_SCOPE is used if an expression
1321132718Skan     like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1322169689Skan     respectively.  QUALIFYING_SCOPE is used for an expression of the
1323132718Skan     form "X::Y"; it refers to X.  */
1324132718Skan  tree object_scope;
1325132718Skan  tree qualifying_scope;
1326132718Skan
1327132718Skan  /* A stack of parsing contexts.  All but the bottom entry on the
1328132718Skan     stack will be tentative contexts.
1329132718Skan
1330132718Skan     We parse tentatively in order to determine which construct is in
1331132718Skan     use in some situations.  For example, in order to determine
1332132718Skan     whether a statement is an expression-statement or a
1333132718Skan     declaration-statement we parse it tentatively as a
1334132718Skan     declaration-statement.  If that fails, we then reparse the same
1335132718Skan     token stream as an expression-statement.  */
1336132718Skan  cp_parser_context *context;
1337132718Skan
1338132718Skan  /* True if we are parsing GNU C++.  If this flag is not set, then
1339132718Skan     GNU extensions are not recognized.  */
1340132718Skan  bool allow_gnu_extensions_p;
1341132718Skan
1342132718Skan  /* TRUE if the `>' token should be interpreted as the greater-than
1343132718Skan     operator.  FALSE if it is the end of a template-id or
1344132718Skan     template-parameter-list.  */
1345132718Skan  bool greater_than_is_operator_p;
1346132718Skan
1347132718Skan  /* TRUE if default arguments are allowed within a parameter list
1348132718Skan     that starts at this point. FALSE if only a gnu extension makes
1349132718Skan     them permissible.  */
1350132718Skan  bool default_arg_ok_p;
1351169689Skan
1352132718Skan  /* TRUE if we are parsing an integral constant-expression.  See
1353132718Skan     [expr.const] for a precise definition.  */
1354132718Skan  bool integral_constant_expression_p;
1355132718Skan
1356132718Skan  /* TRUE if we are parsing an integral constant-expression -- but a
1357132718Skan     non-constant expression should be permitted as well.  This flag
1358132718Skan     is used when parsing an array bound so that GNU variable-length
1359132718Skan     arrays are tolerated.  */
1360132718Skan  bool allow_non_integral_constant_expression_p;
1361132718Skan
1362132718Skan  /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1363132718Skan     been seen that makes the expression non-constant.  */
1364132718Skan  bool non_integral_constant_expression_p;
1365132718Skan
1366132718Skan  /* TRUE if local variable names and `this' are forbidden in the
1367132718Skan     current context.  */
1368132718Skan  bool local_variables_forbidden_p;
1369132718Skan
1370132718Skan  /* TRUE if the declaration we are parsing is part of a
1371132718Skan     linkage-specification of the form `extern string-literal
1372132718Skan     declaration'.  */
1373132718Skan  bool in_unbraced_linkage_specification_p;
1374132718Skan
1375132718Skan  /* TRUE if we are presently parsing a declarator, after the
1376132718Skan     direct-declarator.  */
1377132718Skan  bool in_declarator_p;
1378132718Skan
1379132718Skan  /* TRUE if we are presently parsing a template-argument-list.  */
1380132718Skan  bool in_template_argument_list_p;
1381132718Skan
1382169689Skan  /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1383169689Skan     to IN_OMP_BLOCK if parsing OpenMP structured block and
1384169689Skan     IN_OMP_FOR if parsing OpenMP loop.  If parsing a switch statement,
1385169689Skan     this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1386169689Skan     iteration-statement, OpenMP block or loop within that switch.  */
1387169689Skan#define IN_SWITCH_STMT		1
1388169689Skan#define IN_ITERATION_STMT	2
1389169689Skan#define IN_OMP_BLOCK		4
1390169689Skan#define IN_OMP_FOR		8
1391169689Skan  unsigned char in_statement;
1392132718Skan
1393169689Skan  /* TRUE if we are presently parsing the body of a switch statement.
1394169689Skan     Note that this doesn't quite overlap with in_statement above.
1395169689Skan     The difference relates to giving the right sets of error messages:
1396169689Skan     "case not in switch" vs "break statement used with OpenMP...".  */
1397132718Skan  bool in_switch_statement_p;
1398132718Skan
1399132718Skan  /* TRUE if we are parsing a type-id in an expression context.  In
1400132718Skan     such a situation, both "type (expr)" and "type (type)" are valid
1401132718Skan     alternatives.  */
1402132718Skan  bool in_type_id_in_expr_p;
1403132718Skan
1404169689Skan  /* TRUE if we are currently in a header file where declarations are
1405169689Skan     implicitly extern "C".  */
1406169689Skan  bool implicit_extern_c;
1407169689Skan
1408169689Skan  /* TRUE if strings in expressions should be translated to the execution
1409169689Skan     character set.  */
1410169689Skan  bool translate_strings_p;
1411169689Skan
1412169689Skan  /* TRUE if we are presently parsing the body of a function, but not
1413169689Skan     a local class.  */
1414169689Skan  bool in_function_body;
1415169689Skan
1416132718Skan  /* If non-NULL, then we are parsing a construct where new type
1417132718Skan     definitions are not permitted.  The string stored here will be
1418132718Skan     issued as an error message if a type is defined.  */
1419132718Skan  const char *type_definition_forbidden_message;
1420132718Skan
1421132718Skan  /* A list of lists. The outer list is a stack, used for member
1422132718Skan     functions of local classes. At each level there are two sub-list,
1423132718Skan     one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1424132718Skan     sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1425132718Skan     TREE_VALUE's. The functions are chained in reverse declaration
1426132718Skan     order.
1427132718Skan
1428132718Skan     The TREE_PURPOSE sublist contains those functions with default
1429132718Skan     arguments that need post processing, and the TREE_VALUE sublist
1430132718Skan     contains those functions with definitions that need post
1431132718Skan     processing.
1432132718Skan
1433132718Skan     These lists can only be processed once the outermost class being
1434132718Skan     defined is complete.  */
1435132718Skan  tree unparsed_functions_queues;
1436132718Skan
1437132718Skan  /* The number of classes whose definitions are currently in
1438132718Skan     progress.  */
1439132718Skan  unsigned num_classes_being_defined;
1440132718Skan
1441132718Skan  /* The number of template parameter lists that apply directly to the
1442132718Skan     current declaration.  */
1443132718Skan  unsigned num_template_parameter_lists;
1444132718Skan} cp_parser;
1445132718Skan
1446132718Skan/* Prototypes.  */
1447132718Skan
1448132718Skan/* Constructors and destructors.  */
1449132718Skan
1450132718Skanstatic cp_parser *cp_parser_new
1451132718Skan  (void);
1452132718Skan
1453169689Skan/* Routines to parse various constructs.
1454132718Skan
1455132718Skan   Those that return `tree' will return the error_mark_node (rather
1456132718Skan   than NULL_TREE) if a parse error occurs, unless otherwise noted.
1457132718Skan   Sometimes, they will return an ordinary node if error-recovery was
1458132718Skan   attempted, even though a parse error occurred.  So, to check
1459132718Skan   whether or not a parse error occurred, you should always use
1460132718Skan   cp_parser_error_occurred.  If the construct is optional (indicated
1461132718Skan   either by an `_opt' in the name of the function that does the
1462132718Skan   parsing or via a FLAGS parameter), then NULL_TREE is returned if
1463132718Skan   the construct is not present.  */
1464132718Skan
1465132718Skan/* Lexical conventions [gram.lex]  */
1466132718Skan
1467132718Skanstatic tree cp_parser_identifier
1468132718Skan  (cp_parser *);
1469169689Skanstatic tree cp_parser_string_literal
1470169689Skan  (cp_parser *, bool, bool);
1471132718Skan
1472132718Skan/* Basic concepts [gram.basic]  */
1473132718Skan
1474132718Skanstatic bool cp_parser_translation_unit
1475132718Skan  (cp_parser *);
1476132718Skan
1477132718Skan/* Expressions [gram.expr]  */
1478132718Skan
1479132718Skanstatic tree cp_parser_primary_expression
1480169689Skan  (cp_parser *, bool, bool, bool, cp_id_kind *);
1481132718Skanstatic tree cp_parser_id_expression
1482169689Skan  (cp_parser *, bool, bool, bool *, bool, bool);
1483132718Skanstatic tree cp_parser_unqualified_id
1484169689Skan  (cp_parser *, bool, bool, bool, bool);
1485132718Skanstatic tree cp_parser_nested_name_specifier_opt
1486132718Skan  (cp_parser *, bool, bool, bool, bool);
1487132718Skanstatic tree cp_parser_nested_name_specifier
1488132718Skan  (cp_parser *, bool, bool, bool, bool);
1489132718Skanstatic tree cp_parser_class_or_namespace_name
1490132718Skan  (cp_parser *, bool, bool, bool, bool, bool);
1491132718Skanstatic tree cp_parser_postfix_expression
1492169689Skan  (cp_parser *, bool, bool);
1493169689Skanstatic tree cp_parser_postfix_open_square_expression
1494169689Skan  (cp_parser *, tree, bool);
1495169689Skanstatic tree cp_parser_postfix_dot_deref_expression
1496169689Skan  (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *);
1497132718Skanstatic tree cp_parser_parenthesized_expression_list
1498169689Skan  (cp_parser *, bool, bool, bool *);
1499132718Skanstatic void cp_parser_pseudo_destructor_name
1500132718Skan  (cp_parser *, tree *, tree *);
1501132718Skanstatic tree cp_parser_unary_expression
1502169689Skan  (cp_parser *, bool, bool);
1503132718Skanstatic enum tree_code cp_parser_unary_operator
1504132718Skan  (cp_token *);
1505132718Skanstatic tree cp_parser_new_expression
1506132718Skan  (cp_parser *);
1507132718Skanstatic tree cp_parser_new_placement
1508132718Skan  (cp_parser *);
1509132718Skanstatic tree cp_parser_new_type_id
1510169689Skan  (cp_parser *, tree *);
1511169689Skanstatic cp_declarator *cp_parser_new_declarator_opt
1512132718Skan  (cp_parser *);
1513169689Skanstatic cp_declarator *cp_parser_direct_new_declarator
1514132718Skan  (cp_parser *);
1515132718Skanstatic tree cp_parser_new_initializer
1516132718Skan  (cp_parser *);
1517132718Skanstatic tree cp_parser_delete_expression
1518132718Skan  (cp_parser *);
1519169689Skanstatic tree cp_parser_cast_expression
1520169689Skan  (cp_parser *, bool, bool);
1521169689Skanstatic tree cp_parser_binary_expression
1522132718Skan  (cp_parser *, bool);
1523132718Skanstatic tree cp_parser_question_colon_clause
1524132718Skan  (cp_parser *, tree);
1525132718Skanstatic tree cp_parser_assignment_expression
1526169689Skan  (cp_parser *, bool);
1527132718Skanstatic enum tree_code cp_parser_assignment_operator_opt
1528132718Skan  (cp_parser *);
1529132718Skanstatic tree cp_parser_expression
1530169689Skan  (cp_parser *, bool);
1531132718Skanstatic tree cp_parser_constant_expression
1532132718Skan  (cp_parser *, bool, bool *);
1533169689Skanstatic tree cp_parser_builtin_offsetof
1534169689Skan  (cp_parser *);
1535132718Skan
1536132718Skan/* Statements [gram.stmt.stmt]  */
1537132718Skan
1538132718Skanstatic void cp_parser_statement
1539169689Skan  (cp_parser *, tree, bool);
1540169689Skanstatic void cp_parser_label_for_labeled_statement
1541169689Skan  (cp_parser *);
1542132718Skanstatic tree cp_parser_expression_statement
1543169689Skan  (cp_parser *, tree);
1544132718Skanstatic tree cp_parser_compound_statement
1545169689Skan  (cp_parser *, tree, bool);
1546132718Skanstatic void cp_parser_statement_seq_opt
1547169689Skan  (cp_parser *, tree);
1548132718Skanstatic tree cp_parser_selection_statement
1549132718Skan  (cp_parser *);
1550132718Skanstatic tree cp_parser_condition
1551132718Skan  (cp_parser *);
1552132718Skanstatic tree cp_parser_iteration_statement
1553132718Skan  (cp_parser *);
1554132718Skanstatic void cp_parser_for_init_statement
1555132718Skan  (cp_parser *);
1556132718Skanstatic tree cp_parser_jump_statement
1557132718Skan  (cp_parser *);
1558132718Skanstatic void cp_parser_declaration_statement
1559132718Skan  (cp_parser *);
1560132718Skan
1561132718Skanstatic tree cp_parser_implicitly_scoped_statement
1562132718Skan  (cp_parser *);
1563132718Skanstatic void cp_parser_already_scoped_statement
1564132718Skan  (cp_parser *);
1565132718Skan
1566132718Skan/* Declarations [gram.dcl.dcl] */
1567132718Skan
1568132718Skanstatic void cp_parser_declaration_seq_opt
1569132718Skan  (cp_parser *);
1570132718Skanstatic void cp_parser_declaration
1571132718Skan  (cp_parser *);
1572132718Skanstatic void cp_parser_block_declaration
1573132718Skan  (cp_parser *, bool);
1574132718Skanstatic void cp_parser_simple_declaration
1575132718Skan  (cp_parser *, bool);
1576169689Skanstatic void cp_parser_decl_specifier_seq
1577169689Skan  (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1578132718Skanstatic tree cp_parser_storage_class_specifier_opt
1579132718Skan  (cp_parser *);
1580132718Skanstatic tree cp_parser_function_specifier_opt
1581169689Skan  (cp_parser *, cp_decl_specifier_seq *);
1582132718Skanstatic tree cp_parser_type_specifier
1583169689Skan  (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1584169689Skan   int *, bool *);
1585132718Skanstatic tree cp_parser_simple_type_specifier
1586169689Skan  (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1587132718Skanstatic tree cp_parser_type_name
1588132718Skan  (cp_parser *);
1589132718Skanstatic tree cp_parser_elaborated_type_specifier
1590132718Skan  (cp_parser *, bool, bool);
1591132718Skanstatic tree cp_parser_enum_specifier
1592132718Skan  (cp_parser *);
1593132718Skanstatic void cp_parser_enumerator_list
1594132718Skan  (cp_parser *, tree);
1595169689Skanstatic void cp_parser_enumerator_definition
1596132718Skan  (cp_parser *, tree);
1597132718Skanstatic tree cp_parser_namespace_name
1598132718Skan  (cp_parser *);
1599132718Skanstatic void cp_parser_namespace_definition
1600132718Skan  (cp_parser *);
1601132718Skanstatic void cp_parser_namespace_body
1602132718Skan  (cp_parser *);
1603132718Skanstatic tree cp_parser_qualified_namespace_specifier
1604132718Skan  (cp_parser *);
1605132718Skanstatic void cp_parser_namespace_alias_definition
1606132718Skan  (cp_parser *);
1607169689Skanstatic bool cp_parser_using_declaration
1608169689Skan  (cp_parser *, bool);
1609132718Skanstatic void cp_parser_using_directive
1610132718Skan  (cp_parser *);
1611132718Skanstatic void cp_parser_asm_definition
1612132718Skan  (cp_parser *);
1613132718Skanstatic void cp_parser_linkage_specification
1614132718Skan  (cp_parser *);
1615132718Skan
1616132718Skan/* Declarators [gram.dcl.decl] */
1617132718Skan
1618132718Skanstatic tree cp_parser_init_declarator
1619169689Skan  (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *);
1620169689Skanstatic cp_declarator *cp_parser_declarator
1621146895Skan  (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1622169689Skanstatic cp_declarator *cp_parser_direct_declarator
1623146895Skan  (cp_parser *, cp_parser_declarator_kind, int *, bool);
1624132718Skanstatic enum tree_code cp_parser_ptr_operator
1625169689Skan  (cp_parser *, tree *, cp_cv_quals *);
1626169689Skanstatic cp_cv_quals cp_parser_cv_qualifier_seq_opt
1627132718Skan  (cp_parser *);
1628132718Skanstatic tree cp_parser_declarator_id
1629169689Skan  (cp_parser *, bool);
1630132718Skanstatic tree cp_parser_type_id
1631132718Skan  (cp_parser *);
1632169689Skanstatic void cp_parser_type_specifier_seq
1633169689Skan  (cp_parser *, bool, cp_decl_specifier_seq *);
1634169689Skanstatic cp_parameter_declarator *cp_parser_parameter_declaration_clause
1635132718Skan  (cp_parser *);
1636169689Skanstatic cp_parameter_declarator *cp_parser_parameter_declaration_list
1637169689Skan  (cp_parser *, bool *);
1638169689Skanstatic cp_parameter_declarator *cp_parser_parameter_declaration
1639132718Skan  (cp_parser *, bool, bool *);
1640132718Skanstatic void cp_parser_function_body
1641132718Skan  (cp_parser *);
1642132718Skanstatic tree cp_parser_initializer
1643132718Skan  (cp_parser *, bool *, bool *);
1644132718Skanstatic tree cp_parser_initializer_clause
1645132718Skan  (cp_parser *, bool *);
1646169689Skanstatic VEC(constructor_elt,gc) *cp_parser_initializer_list
1647132718Skan  (cp_parser *, bool *);
1648132718Skan
1649132718Skanstatic bool cp_parser_ctor_initializer_opt_and_function_body
1650132718Skan  (cp_parser *);
1651132718Skan
1652132718Skan/* Classes [gram.class] */
1653132718Skan
1654132718Skanstatic tree cp_parser_class_name
1655169689Skan  (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1656132718Skanstatic tree cp_parser_class_specifier
1657132718Skan  (cp_parser *);
1658132718Skanstatic tree cp_parser_class_head
1659169689Skan  (cp_parser *, bool *, tree *, tree *);
1660132718Skanstatic enum tag_types cp_parser_class_key
1661132718Skan  (cp_parser *);
1662132718Skanstatic void cp_parser_member_specification_opt
1663132718Skan  (cp_parser *);
1664132718Skanstatic void cp_parser_member_declaration
1665132718Skan  (cp_parser *);
1666132718Skanstatic tree cp_parser_pure_specifier
1667132718Skan  (cp_parser *);
1668132718Skanstatic tree cp_parser_constant_initializer
1669132718Skan  (cp_parser *);
1670132718Skan
1671132718Skan/* Derived classes [gram.class.derived] */
1672132718Skan
1673132718Skanstatic tree cp_parser_base_clause
1674132718Skan  (cp_parser *);
1675132718Skanstatic tree cp_parser_base_specifier
1676132718Skan  (cp_parser *);
1677132718Skan
1678132718Skan/* Special member functions [gram.special] */
1679132718Skan
1680132718Skanstatic tree cp_parser_conversion_function_id
1681132718Skan  (cp_parser *);
1682132718Skanstatic tree cp_parser_conversion_type_id
1683132718Skan  (cp_parser *);
1684169689Skanstatic cp_declarator *cp_parser_conversion_declarator_opt
1685132718Skan  (cp_parser *);
1686132718Skanstatic bool cp_parser_ctor_initializer_opt
1687132718Skan  (cp_parser *);
1688132718Skanstatic void cp_parser_mem_initializer_list
1689132718Skan  (cp_parser *);
1690132718Skanstatic tree cp_parser_mem_initializer
1691132718Skan  (cp_parser *);
1692132718Skanstatic tree cp_parser_mem_initializer_id
1693132718Skan  (cp_parser *);
1694132718Skan
1695132718Skan/* Overloading [gram.over] */
1696132718Skan
1697132718Skanstatic tree cp_parser_operator_function_id
1698132718Skan  (cp_parser *);
1699132718Skanstatic tree cp_parser_operator
1700132718Skan  (cp_parser *);
1701132718Skan
1702132718Skan/* Templates [gram.temp] */
1703132718Skan
1704132718Skanstatic void cp_parser_template_declaration
1705132718Skan  (cp_parser *, bool);
1706132718Skanstatic tree cp_parser_template_parameter_list
1707132718Skan  (cp_parser *);
1708132718Skanstatic tree cp_parser_template_parameter
1709169689Skan  (cp_parser *, bool *);
1710132718Skanstatic tree cp_parser_type_parameter
1711132718Skan  (cp_parser *);
1712132718Skanstatic tree cp_parser_template_id
1713132718Skan  (cp_parser *, bool, bool, bool);
1714132718Skanstatic tree cp_parser_template_name
1715132718Skan  (cp_parser *, bool, bool, bool, bool *);
1716132718Skanstatic tree cp_parser_template_argument_list
1717132718Skan  (cp_parser *);
1718132718Skanstatic tree cp_parser_template_argument
1719132718Skan  (cp_parser *);
1720132718Skanstatic void cp_parser_explicit_instantiation
1721132718Skan  (cp_parser *);
1722132718Skanstatic void cp_parser_explicit_specialization
1723132718Skan  (cp_parser *);
1724132718Skan
1725132718Skan/* Exception handling [gram.exception] */
1726132718Skan
1727169689Skanstatic tree cp_parser_try_block
1728132718Skan  (cp_parser *);
1729132718Skanstatic bool cp_parser_function_try_block
1730132718Skan  (cp_parser *);
1731132718Skanstatic void cp_parser_handler_seq
1732132718Skan  (cp_parser *);
1733132718Skanstatic void cp_parser_handler
1734132718Skan  (cp_parser *);
1735132718Skanstatic tree cp_parser_exception_declaration
1736132718Skan  (cp_parser *);
1737132718Skanstatic tree cp_parser_throw_expression
1738132718Skan  (cp_parser *);
1739132718Skanstatic tree cp_parser_exception_specification_opt
1740132718Skan  (cp_parser *);
1741132718Skanstatic tree cp_parser_type_id_list
1742132718Skan  (cp_parser *);
1743132718Skan
1744132718Skan/* GNU Extensions */
1745132718Skan
1746132718Skanstatic tree cp_parser_asm_specification_opt
1747132718Skan  (cp_parser *);
1748132718Skanstatic tree cp_parser_asm_operand_list
1749132718Skan  (cp_parser *);
1750132718Skanstatic tree cp_parser_asm_clobber_list
1751132718Skan  (cp_parser *);
1752132718Skanstatic tree cp_parser_attributes_opt
1753132718Skan  (cp_parser *);
1754132718Skanstatic tree cp_parser_attribute_list
1755132718Skan  (cp_parser *);
1756132718Skanstatic bool cp_parser_extension_opt
1757132718Skan  (cp_parser *, int *);
1758132718Skanstatic void cp_parser_label_declaration
1759132718Skan  (cp_parser *);
1760132718Skan
1761169689Skanenum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1762169689Skanstatic bool cp_parser_pragma
1763169689Skan  (cp_parser *, enum pragma_context);
1764169689Skan
1765169689Skan/* Objective-C++ Productions */
1766169689Skan
1767169689Skanstatic tree cp_parser_objc_message_receiver
1768169689Skan  (cp_parser *);
1769169689Skanstatic tree cp_parser_objc_message_args
1770169689Skan  (cp_parser *);
1771169689Skanstatic tree cp_parser_objc_message_expression
1772169689Skan  (cp_parser *);
1773169689Skanstatic tree cp_parser_objc_encode_expression
1774169689Skan  (cp_parser *);
1775169689Skanstatic tree cp_parser_objc_defs_expression
1776169689Skan  (cp_parser *);
1777169689Skanstatic tree cp_parser_objc_protocol_expression
1778169689Skan  (cp_parser *);
1779169689Skanstatic tree cp_parser_objc_selector_expression
1780169689Skan  (cp_parser *);
1781169689Skanstatic tree cp_parser_objc_expression
1782169689Skan  (cp_parser *);
1783169689Skanstatic bool cp_parser_objc_selector_p
1784169689Skan  (enum cpp_ttype);
1785169689Skanstatic tree cp_parser_objc_selector
1786169689Skan  (cp_parser *);
1787169689Skanstatic tree cp_parser_objc_protocol_refs_opt
1788169689Skan  (cp_parser *);
1789169689Skanstatic void cp_parser_objc_declaration
1790169689Skan  (cp_parser *);
1791169689Skanstatic tree cp_parser_objc_statement
1792169689Skan  (cp_parser *);
1793169689Skan
1794132718Skan/* Utility Routines */
1795132718Skan
1796132718Skanstatic tree cp_parser_lookup_name
1797169689Skan  (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *);
1798132718Skanstatic tree cp_parser_lookup_name_simple
1799132718Skan  (cp_parser *, tree);
1800132718Skanstatic tree cp_parser_maybe_treat_template_as_class
1801132718Skan  (tree, bool);
1802132718Skanstatic bool cp_parser_check_declarator_template_parameters
1803169689Skan  (cp_parser *, cp_declarator *);
1804132718Skanstatic bool cp_parser_check_template_parameters
1805132718Skan  (cp_parser *, unsigned);
1806132718Skanstatic tree cp_parser_simple_cast_expression
1807132718Skan  (cp_parser *);
1808132718Skanstatic tree cp_parser_global_scope_opt
1809132718Skan  (cp_parser *, bool);
1810132718Skanstatic bool cp_parser_constructor_declarator_p
1811132718Skan  (cp_parser *, bool);
1812132718Skanstatic tree cp_parser_function_definition_from_specifiers_and_declarator
1813169689Skan  (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1814132718Skanstatic tree cp_parser_function_definition_after_declarator
1815132718Skan  (cp_parser *, bool);
1816132718Skanstatic void cp_parser_template_declaration_after_export
1817132718Skan  (cp_parser *, bool);
1818169689Skanstatic void cp_parser_perform_template_parameter_access_checks
1819169689Skan  (VEC (deferred_access_check,gc)*);
1820132718Skanstatic tree cp_parser_single_declaration
1821169689Skan  (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool *);
1822132718Skanstatic tree cp_parser_functional_cast
1823132718Skan  (cp_parser *, tree);
1824132718Skanstatic tree cp_parser_save_member_function_body
1825169689Skan  (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1826132718Skanstatic tree cp_parser_enclosed_template_argument_list
1827132718Skan  (cp_parser *);
1828132718Skanstatic void cp_parser_save_default_args
1829132718Skan  (cp_parser *, tree);
1830132718Skanstatic void cp_parser_late_parsing_for_member
1831132718Skan  (cp_parser *, tree);
1832132718Skanstatic void cp_parser_late_parsing_default_args
1833132718Skan  (cp_parser *, tree);
1834132718Skanstatic tree cp_parser_sizeof_operand
1835132718Skan  (cp_parser *, enum rid);
1836132718Skanstatic bool cp_parser_declares_only_class_p
1837132718Skan  (cp_parser *);
1838169689Skanstatic void cp_parser_set_storage_class
1839169689Skan  (cp_parser *, cp_decl_specifier_seq *, enum rid);
1840169689Skanstatic void cp_parser_set_decl_spec_type
1841169689Skan  (cp_decl_specifier_seq *, tree, bool);
1842132718Skanstatic bool cp_parser_friend_p
1843169689Skan  (const cp_decl_specifier_seq *);
1844132718Skanstatic cp_token *cp_parser_require
1845132718Skan  (cp_parser *, enum cpp_ttype, const char *);
1846132718Skanstatic cp_token *cp_parser_require_keyword
1847132718Skan  (cp_parser *, enum rid, const char *);
1848169689Skanstatic bool cp_parser_token_starts_function_definition_p
1849132718Skan  (cp_token *);
1850132718Skanstatic bool cp_parser_next_token_starts_class_definition_p
1851132718Skan  (cp_parser *);
1852132718Skanstatic bool cp_parser_next_token_ends_template_argument_p
1853132718Skan  (cp_parser *);
1854132718Skanstatic bool cp_parser_nth_token_starts_template_argument_list_p
1855132718Skan  (cp_parser *, size_t);
1856132718Skanstatic enum tag_types cp_parser_token_is_class_key
1857132718Skan  (cp_token *);
1858132718Skanstatic void cp_parser_check_class_key
1859132718Skan  (enum tag_types, tree type);
1860132718Skanstatic void cp_parser_check_access_in_redeclaration
1861132718Skan  (tree type);
1862132718Skanstatic bool cp_parser_optional_template_keyword
1863132718Skan  (cp_parser *);
1864169689Skanstatic void cp_parser_pre_parsed_nested_name_specifier
1865132718Skan  (cp_parser *);
1866132718Skanstatic void cp_parser_cache_group
1867169689Skan  (cp_parser *, enum cpp_ttype, unsigned);
1868169689Skanstatic void cp_parser_parse_tentatively
1869132718Skan  (cp_parser *);
1870132718Skanstatic void cp_parser_commit_to_tentative_parse
1871132718Skan  (cp_parser *);
1872132718Skanstatic void cp_parser_abort_tentative_parse
1873132718Skan  (cp_parser *);
1874132718Skanstatic bool cp_parser_parse_definitely
1875132718Skan  (cp_parser *);
1876132718Skanstatic inline bool cp_parser_parsing_tentatively
1877132718Skan  (cp_parser *);
1878169689Skanstatic bool cp_parser_uncommitted_to_tentative_parse_p
1879132718Skan  (cp_parser *);
1880132718Skanstatic void cp_parser_error
1881132718Skan  (cp_parser *, const char *);
1882132718Skanstatic void cp_parser_name_lookup_error
1883132718Skan  (cp_parser *, tree, tree, const char *);
1884132718Skanstatic bool cp_parser_simulate_error
1885132718Skan  (cp_parser *);
1886169689Skanstatic bool cp_parser_check_type_definition
1887132718Skan  (cp_parser *);
1888132718Skanstatic void cp_parser_check_for_definition_in_return_type
1889169689Skan  (cp_declarator *, tree);
1890132718Skanstatic void cp_parser_check_for_invalid_template_id
1891132718Skan  (cp_parser *, tree);
1892132718Skanstatic bool cp_parser_non_integral_constant_expression
1893132718Skan  (cp_parser *, const char *);
1894169689Skanstatic void cp_parser_diagnose_invalid_type_name
1895169689Skan  (cp_parser *, tree, tree);
1896169689Skanstatic bool cp_parser_parse_and_diagnose_invalid_type_name
1897132718Skan  (cp_parser *);
1898132718Skanstatic int cp_parser_skip_to_closing_parenthesis
1899132718Skan  (cp_parser *, bool, bool, bool);
1900132718Skanstatic void cp_parser_skip_to_end_of_statement
1901132718Skan  (cp_parser *);
1902132718Skanstatic void cp_parser_consume_semicolon_at_end_of_statement
1903132718Skan  (cp_parser *);
1904132718Skanstatic void cp_parser_skip_to_end_of_block_or_statement
1905132718Skan  (cp_parser *);
1906132718Skanstatic void cp_parser_skip_to_closing_brace
1907132718Skan  (cp_parser *);
1908169689Skanstatic void cp_parser_skip_to_end_of_template_parameter_list
1909169689Skan  (cp_parser *);
1910169689Skanstatic void cp_parser_skip_to_pragma_eol
1911169689Skan  (cp_parser*, cp_token *);
1912132718Skanstatic bool cp_parser_error_occurred
1913132718Skan  (cp_parser *);
1914132718Skanstatic bool cp_parser_allow_gnu_extensions_p
1915132718Skan  (cp_parser *);
1916132718Skanstatic bool cp_parser_is_string_literal
1917132718Skan  (cp_token *);
1918169689Skanstatic bool cp_parser_is_keyword
1919132718Skan  (cp_token *, enum rid);
1920169689Skanstatic tree cp_parser_make_typename_type
1921169689Skan  (cp_parser *, tree, tree);
1922132718Skan
1923132718Skan/* Returns nonzero if we are parsing tentatively.  */
1924132718Skan
1925132718Skanstatic inline bool
1926132718Skancp_parser_parsing_tentatively (cp_parser* parser)
1927132718Skan{
1928132718Skan  return parser->context->next != NULL;
1929132718Skan}
1930132718Skan
1931132718Skan/* Returns nonzero if TOKEN is a string literal.  */
1932132718Skan
1933132718Skanstatic bool
1934132718Skancp_parser_is_string_literal (cp_token* token)
1935132718Skan{
1936132718Skan  return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1937132718Skan}
1938132718Skan
1939132718Skan/* Returns nonzero if TOKEN is the indicated KEYWORD.  */
1940132718Skan
1941132718Skanstatic bool
1942132718Skancp_parser_is_keyword (cp_token* token, enum rid keyword)
1943132718Skan{
1944132718Skan  return token->keyword == keyword;
1945132718Skan}
1946132718Skan
1947169689Skan/* If not parsing tentatively, issue a diagnostic of the form
1948169689Skan      FILE:LINE: MESSAGE before TOKEN
1949169689Skan   where TOKEN is the next token in the input stream.  MESSAGE
1950169689Skan   (specified by the caller) is usually of the form "expected
1951169689Skan   OTHER-TOKEN".  */
1952132718Skan
1953132718Skanstatic void
1954132718Skancp_parser_error (cp_parser* parser, const char* message)
1955132718Skan{
1956132718Skan  if (!cp_parser_simulate_error (parser))
1957132718Skan    {
1958169689Skan      cp_token *token = cp_lexer_peek_token (parser->lexer);
1959169689Skan      /* This diagnostic makes more sense if it is tagged to the line
1960169689Skan	 of the token we just peeked at.  */
1961169689Skan      cp_lexer_set_source_position_from_token (token);
1962169689Skan
1963169689Skan      if (token->type == CPP_PRAGMA)
1964169689Skan	{
1965169689Skan	  error ("%<#pragma%> is not allowed here");
1966169689Skan	  cp_parser_skip_to_pragma_eol (parser, token);
1967169689Skan	  return;
1968169689Skan	}
1969169689Skan
1970169689Skan      c_parse_error (message,
1971132718Skan		     /* Because c_parser_error does not understand
1972132718Skan			CPP_KEYWORD, keywords are treated like
1973132718Skan			identifiers.  */
1974169689Skan		     (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
1975169689Skan		     token->u.value);
1976132718Skan    }
1977132718Skan}
1978132718Skan
1979132718Skan/* Issue an error about name-lookup failing.  NAME is the
1980132718Skan   IDENTIFIER_NODE DECL is the result of
1981132718Skan   the lookup (as returned from cp_parser_lookup_name).  DESIRED is
1982132718Skan   the thing that we hoped to find.  */
1983132718Skan
1984132718Skanstatic void
1985132718Skancp_parser_name_lookup_error (cp_parser* parser,
1986132718Skan			     tree name,
1987132718Skan			     tree decl,
1988132718Skan			     const char* desired)
1989132718Skan{
1990132718Skan  /* If name lookup completely failed, tell the user that NAME was not
1991132718Skan     declared.  */
1992132718Skan  if (decl == error_mark_node)
1993132718Skan    {
1994132718Skan      if (parser->scope && parser->scope != global_namespace)
1995169689Skan	error ("%<%D::%D%> has not been declared",
1996132718Skan	       parser->scope, name);
1997132718Skan      else if (parser->scope == global_namespace)
1998169689Skan	error ("%<::%D%> has not been declared", name);
1999169689Skan      else if (parser->object_scope
2000169689Skan	       && !CLASS_TYPE_P (parser->object_scope))
2001169689Skan	error ("request for member %qD in non-class type %qT",
2002169689Skan	       name, parser->object_scope);
2003169689Skan      else if (parser->object_scope)
2004169689Skan	error ("%<%T::%D%> has not been declared",
2005169689Skan	       parser->object_scope, name);
2006132718Skan      else
2007169689Skan	error ("%qD has not been declared", name);
2008132718Skan    }
2009132718Skan  else if (parser->scope && parser->scope != global_namespace)
2010169689Skan    error ("%<%D::%D%> %s", parser->scope, name, desired);
2011132718Skan  else if (parser->scope == global_namespace)
2012169689Skan    error ("%<::%D%> %s", name, desired);
2013132718Skan  else
2014169689Skan    error ("%qD %s", name, desired);
2015132718Skan}
2016132718Skan
2017132718Skan/* If we are parsing tentatively, remember that an error has occurred
2018132718Skan   during this tentative parse.  Returns true if the error was
2019169689Skan   simulated; false if a message should be issued by the caller.  */
2020132718Skan
2021132718Skanstatic bool
2022132718Skancp_parser_simulate_error (cp_parser* parser)
2023132718Skan{
2024169689Skan  if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2025132718Skan    {
2026132718Skan      parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2027132718Skan      return true;
2028132718Skan    }
2029132718Skan  return false;
2030132718Skan}
2031132718Skan
2032169689Skan/* Check for repeated decl-specifiers.  */
2033169689Skan
2034169689Skanstatic void
2035169689Skancp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs)
2036169689Skan{
2037169689Skan  cp_decl_spec ds;
2038169689Skan
2039169689Skan  for (ds = ds_first; ds != ds_last; ++ds)
2040169689Skan    {
2041169689Skan      unsigned count = decl_specs->specs[(int)ds];
2042169689Skan      if (count < 2)
2043169689Skan	continue;
2044169689Skan      /* The "long" specifier is a special case because of "long long".  */
2045169689Skan      if (ds == ds_long)
2046169689Skan	{
2047169689Skan	  if (count > 2)
2048169689Skan	    error ("%<long long long%> is too long for GCC");
2049169689Skan	  else if (pedantic && !in_system_header && warn_long_long)
2050169689Skan	    pedwarn ("ISO C++ does not support %<long long%>");
2051169689Skan	}
2052169689Skan      else if (count > 1)
2053169689Skan	{
2054169689Skan	  static const char *const decl_spec_names[] = {
2055169689Skan	    "signed",
2056169689Skan	    "unsigned",
2057169689Skan	    "short",
2058169689Skan	    "long",
2059169689Skan	    "const",
2060169689Skan	    "volatile",
2061169689Skan	    "restrict",
2062169689Skan	    "inline",
2063169689Skan	    "virtual",
2064169689Skan	    "explicit",
2065169689Skan	    "friend",
2066169689Skan	    "typedef",
2067169689Skan	    "__complex",
2068169689Skan	    "__thread"
2069169689Skan	  };
2070169689Skan	  error ("duplicate %qs", decl_spec_names[(int)ds]);
2071169689Skan	}
2072169689Skan    }
2073169689Skan}
2074169689Skan
2075132718Skan/* This function is called when a type is defined.  If type
2076132718Skan   definitions are forbidden at this point, an error message is
2077132718Skan   issued.  */
2078132718Skan
2079169689Skanstatic bool
2080132718Skancp_parser_check_type_definition (cp_parser* parser)
2081132718Skan{
2082132718Skan  /* If types are forbidden here, issue a message.  */
2083132718Skan  if (parser->type_definition_forbidden_message)
2084169689Skan    {
2085169689Skan      /* Use `%s' to print the string in case there are any escape
2086169689Skan	 characters in the message.  */
2087169689Skan      error ("%s", parser->type_definition_forbidden_message);
2088169689Skan      return false;
2089169689Skan    }
2090169689Skan  return true;
2091132718Skan}
2092132718Skan
2093161651Skan/* This function is called when the DECLARATOR is processed.  The TYPE
2094161651Skan   was a type defined in the decl-specifiers.  If it is invalid to
2095161651Skan   define a type in the decl-specifiers for DECLARATOR, an error is
2096161651Skan   issued.  */
2097132718Skan
2098132718Skanstatic void
2099169689Skancp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2100169689Skan					       tree type)
2101132718Skan{
2102132718Skan  /* [dcl.fct] forbids type definitions in return types.
2103132718Skan     Unfortunately, it's not easy to know whether or not we are
2104132718Skan     processing a return type until after the fact.  */
2105132718Skan  while (declarator
2106169689Skan	 && (declarator->kind == cdk_pointer
2107169689Skan	     || declarator->kind == cdk_reference
2108169689Skan	     || declarator->kind == cdk_ptrmem))
2109169689Skan    declarator = declarator->declarator;
2110132718Skan  if (declarator
2111169689Skan      && declarator->kind == cdk_function)
2112161651Skan    {
2113161651Skan      error ("new types may not be defined in a return type");
2114169689Skan      inform ("(perhaps a semicolon is missing after the definition of %qT)",
2115161651Skan	      type);
2116161651Skan    }
2117132718Skan}
2118132718Skan
2119132718Skan/* A type-specifier (TYPE) has been parsed which cannot be followed by
2120132718Skan   "<" in any valid C++ program.  If the next token is indeed "<",
2121132718Skan   issue a message warning the user about what appears to be an
2122132718Skan   invalid attempt to form a template-id.  */
2123132718Skan
2124132718Skanstatic void
2125169689Skancp_parser_check_for_invalid_template_id (cp_parser* parser,
2126132718Skan					 tree type)
2127132718Skan{
2128169689Skan  cp_token_position start = 0;
2129132718Skan
2130132718Skan  if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2131132718Skan    {
2132132718Skan      if (TYPE_P (type))
2133169689Skan	error ("%qT is not a template", type);
2134132718Skan      else if (TREE_CODE (type) == IDENTIFIER_NODE)
2135169689Skan	error ("%qE is not a template", type);
2136132718Skan      else
2137132718Skan	error ("invalid template-id");
2138132718Skan      /* Remember the location of the invalid "<".  */
2139169689Skan      if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2140169689Skan	start = cp_lexer_token_position (parser->lexer, true);
2141132718Skan      /* Consume the "<".  */
2142132718Skan      cp_lexer_consume_token (parser->lexer);
2143132718Skan      /* Parse the template arguments.  */
2144132718Skan      cp_parser_enclosed_template_argument_list (parser);
2145132718Skan      /* Permanently remove the invalid template arguments so that
2146132718Skan	 this error message is not issued again.  */
2147169689Skan      if (start)
2148169689Skan	cp_lexer_purge_tokens_after (parser->lexer, start);
2149132718Skan    }
2150132718Skan}
2151132718Skan
2152132718Skan/* If parsing an integral constant-expression, issue an error message
2153132718Skan   about the fact that THING appeared and return true.  Otherwise,
2154169689Skan   return false.  In either case, set
2155169689Skan   PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P.  */
2156132718Skan
2157132718Skanstatic bool
2158132718Skancp_parser_non_integral_constant_expression (cp_parser  *parser,
2159132718Skan					    const char *thing)
2160132718Skan{
2161169689Skan  parser->non_integral_constant_expression_p = true;
2162132718Skan  if (parser->integral_constant_expression_p)
2163132718Skan    {
2164132718Skan      if (!parser->allow_non_integral_constant_expression_p)
2165132718Skan	{
2166132718Skan	  error ("%s cannot appear in a constant-expression", thing);
2167132718Skan	  return true;
2168132718Skan	}
2169132718Skan    }
2170132718Skan  return false;
2171132718Skan}
2172132718Skan
2173169689Skan/* Emit a diagnostic for an invalid type name.  SCOPE is the
2174169689Skan   qualifying scope (or NULL, if none) for ID.  This function commits
2175169689Skan   to the current active tentative parse, if any.  (Otherwise, the
2176169689Skan   problematic construct might be encountered again later, resulting
2177169689Skan   in duplicate error messages.)  */
2178132718Skan
2179169689Skanstatic void
2180169689Skancp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id)
2181132718Skan{
2182169689Skan  tree decl, old_scope;
2183169689Skan  /* Try to lookup the identifier.  */
2184169689Skan  old_scope = parser->scope;
2185169689Skan  parser->scope = scope;
2186169689Skan  decl = cp_parser_lookup_name_simple (parser, id);
2187169689Skan  parser->scope = old_scope;
2188169689Skan  /* If the lookup found a template-name, it means that the user forgot
2189169689Skan  to specify an argument list. Emit a useful error message.  */
2190169689Skan  if (TREE_CODE (decl) == TEMPLATE_DECL)
2191169689Skan    error ("invalid use of template-name %qE without an argument list", decl);
2192169689Skan  else if (TREE_CODE (id) == BIT_NOT_EXPR)
2193169689Skan    error ("invalid use of destructor %qD as a type", id);
2194169689Skan  else if (TREE_CODE (decl) == TYPE_DECL)
2195169689Skan    /* Something like 'unsigned A a;'  */
2196169689Skan    error ("invalid combination of multiple type-specifiers");
2197169689Skan  else if (!parser->scope)
2198132718Skan    {
2199132718Skan      /* Issue an error message.  */
2200169689Skan      error ("%qE does not name a type", id);
2201132718Skan      /* If we're in a template class, it's possible that the user was
2202132718Skan	 referring to a type from a base class.  For example:
2203132718Skan
2204132718Skan	   template <typename T> struct A { typedef T X; };
2205132718Skan	   template <typename T> struct B : public A<T> { X x; };
2206132718Skan
2207132718Skan	 The user should have said "typename A<T>::X".  */
2208169689Skan      if (processing_template_decl && current_class_type
2209169689Skan	  && TYPE_BINFO (current_class_type))
2210132718Skan	{
2211132718Skan	  tree b;
2212132718Skan
2213132718Skan	  for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2214132718Skan	       b;
2215132718Skan	       b = TREE_CHAIN (b))
2216132718Skan	    {
2217132718Skan	      tree base_type = BINFO_TYPE (b);
2218169689Skan	      if (CLASS_TYPE_P (base_type)
2219132718Skan		  && dependent_type_p (base_type))
2220132718Skan		{
2221132718Skan		  tree field;
2222132718Skan		  /* Go from a particular instantiation of the
2223132718Skan		     template (which will have an empty TYPE_FIELDs),
2224132718Skan		     to the main version.  */
2225132718Skan		  base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2226132718Skan		  for (field = TYPE_FIELDS (base_type);
2227132718Skan		       field;
2228132718Skan		       field = TREE_CHAIN (field))
2229132718Skan		    if (TREE_CODE (field) == TYPE_DECL
2230169689Skan			&& DECL_NAME (field) == id)
2231132718Skan		      {
2232169689Skan			inform ("(perhaps %<typename %T::%E%> was intended)",
2233169689Skan				BINFO_TYPE (b), id);
2234132718Skan			break;
2235132718Skan		      }
2236132718Skan		  if (field)
2237132718Skan		    break;
2238132718Skan		}
2239132718Skan	    }
2240132718Skan	}
2241132718Skan    }
2242169689Skan  /* Here we diagnose qualified-ids where the scope is actually correct,
2243169689Skan     but the identifier does not resolve to a valid type name.  */
2244169689Skan  else if (parser->scope != error_mark_node)
2245169689Skan    {
2246169689Skan      if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2247169689Skan	error ("%qE in namespace %qE does not name a type",
2248169689Skan	       id, parser->scope);
2249169689Skan      else if (TYPE_P (parser->scope))
2250169689Skan	error ("%qE in class %qT does not name a type", id, parser->scope);
2251169689Skan      else
2252169689Skan	gcc_unreachable ();
2253169689Skan    }
2254169689Skan  cp_parser_commit_to_tentative_parse (parser);
2255169689Skan}
2256132718Skan
2257169689Skan/* Check for a common situation where a type-name should be present,
2258169689Skan   but is not, and issue a sensible error message.  Returns true if an
2259169689Skan   invalid type-name was detected.
2260169689Skan
2261169689Skan   The situation handled by this function are variable declarations of the
2262169689Skan   form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2263169689Skan   Usually, `ID' should name a type, but if we got here it means that it
2264169689Skan   does not. We try to emit the best possible error message depending on
2265169689Skan   how exactly the id-expression looks like.  */
2266169689Skan
2267169689Skanstatic bool
2268169689Skancp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2269169689Skan{
2270169689Skan  tree id;
2271169689Skan
2272169689Skan  cp_parser_parse_tentatively (parser);
2273169689Skan  id = cp_parser_id_expression (parser,
2274169689Skan				/*template_keyword_p=*/false,
2275169689Skan				/*check_dependency_p=*/true,
2276169689Skan				/*template_p=*/NULL,
2277169689Skan				/*declarator_p=*/true,
2278169689Skan				/*optional_p=*/false);
2279169689Skan  /* After the id-expression, there should be a plain identifier,
2280169689Skan     otherwise this is not a simple variable declaration. Also, if
2281169689Skan     the scope is dependent, we cannot do much.  */
2282169689Skan  if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2283169689Skan      || (parser->scope && TYPE_P (parser->scope)
2284169689Skan	  && dependent_type_p (parser->scope))
2285169689Skan      || TREE_CODE (id) == TYPE_DECL)
2286169689Skan    {
2287169689Skan      cp_parser_abort_tentative_parse (parser);
2288169689Skan      return false;
2289169689Skan    }
2290169689Skan  if (!cp_parser_parse_definitely (parser))
2291169689Skan    return false;
2292169689Skan
2293169689Skan  /* Emit a diagnostic for the invalid type.  */
2294169689Skan  cp_parser_diagnose_invalid_type_name (parser, parser->scope, id);
2295169689Skan  /* Skip to the end of the declaration; there's no point in
2296169689Skan     trying to process it.  */
2297169689Skan  cp_parser_skip_to_end_of_block_or_statement (parser);
2298169689Skan  return true;
2299132718Skan}
2300132718Skan
2301169689Skan/* Consume tokens up to, and including, the next non-nested closing `)'.
2302132718Skan   Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
2303132718Skan   are doing error recovery. Returns -1 if OR_COMMA is true and we
2304132718Skan   found an unnested comma.  */
2305132718Skan
2306132718Skanstatic int
2307132718Skancp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2308169689Skan				       bool recovering,
2309132718Skan				       bool or_comma,
2310132718Skan				       bool consume_paren)
2311132718Skan{
2312132718Skan  unsigned paren_depth = 0;
2313132718Skan  unsigned brace_depth = 0;
2314132718Skan
2315169689Skan  if (recovering && !or_comma
2316169689Skan      && cp_parser_uncommitted_to_tentative_parse_p (parser))
2317132718Skan    return 0;
2318169689Skan
2319132718Skan  while (true)
2320132718Skan    {
2321169689Skan      cp_token * token = cp_lexer_peek_token (parser->lexer);
2322132718Skan
2323169689Skan      switch (token->type)
2324132718Skan	{
2325169689Skan	case CPP_EOF:
2326169689Skan	case CPP_PRAGMA_EOL:
2327169689Skan	  /* If we've run out of tokens, then there is no closing `)'.  */
2328169689Skan	  return 0;
2329169689Skan
2330169689Skan	case CPP_SEMICOLON:
2331169689Skan	  /* This matches the processing in skip_to_end_of_statement.  */
2332169689Skan	  if (!brace_depth)
2333169689Skan	    return 0;
2334169689Skan	  break;
2335169689Skan
2336169689Skan	case CPP_OPEN_BRACE:
2337169689Skan	  ++brace_depth;
2338169689Skan	  break;
2339169689Skan	case CPP_CLOSE_BRACE:
2340132718Skan	  if (!brace_depth--)
2341132718Skan	    return 0;
2342169689Skan	  break;
2343169689Skan
2344169689Skan	case CPP_COMMA:
2345169689Skan	  if (recovering && or_comma && !brace_depth && !paren_depth)
2346169689Skan	    return -1;
2347169689Skan	  break;
2348169689Skan
2349169689Skan	case CPP_OPEN_PAREN:
2350169689Skan	  if (!brace_depth)
2351132718Skan	    ++paren_depth;
2352169689Skan	  break;
2353169689Skan
2354169689Skan	case CPP_CLOSE_PAREN:
2355169689Skan	  if (!brace_depth && !paren_depth--)
2356132718Skan	    {
2357132718Skan	      if (consume_paren)
2358132718Skan		cp_lexer_consume_token (parser->lexer);
2359132718Skan	      return 1;
2360132718Skan	    }
2361169689Skan	  break;
2362169689Skan
2363169689Skan	default:
2364169689Skan	  break;
2365132718Skan	}
2366169689Skan
2367132718Skan      /* Consume the token.  */
2368132718Skan      cp_lexer_consume_token (parser->lexer);
2369132718Skan    }
2370132718Skan}
2371132718Skan
2372132718Skan/* Consume tokens until we reach the end of the current statement.
2373132718Skan   Normally, that will be just before consuming a `;'.  However, if a
2374132718Skan   non-nested `}' comes first, then we stop before consuming that.  */
2375132718Skan
2376132718Skanstatic void
2377132718Skancp_parser_skip_to_end_of_statement (cp_parser* parser)
2378132718Skan{
2379132718Skan  unsigned nesting_depth = 0;
2380132718Skan
2381132718Skan  while (true)
2382132718Skan    {
2383169689Skan      cp_token *token = cp_lexer_peek_token (parser->lexer);
2384132718Skan
2385169689Skan      switch (token->type)
2386132718Skan	{
2387169689Skan	case CPP_EOF:
2388169689Skan	case CPP_PRAGMA_EOL:
2389169689Skan	  /* If we've run out of tokens, stop.  */
2390169689Skan	  return;
2391169689Skan
2392169689Skan	case CPP_SEMICOLON:
2393169689Skan	  /* If the next token is a `;', we have reached the end of the
2394169689Skan	     statement.  */
2395169689Skan	  if (!nesting_depth)
2396169689Skan	    return;
2397169689Skan	  break;
2398169689Skan
2399169689Skan	case CPP_CLOSE_BRACE:
2400169689Skan	  /* If this is a non-nested '}', stop before consuming it.
2401132718Skan	     That way, when confronted with something like:
2402132718Skan
2403169689Skan	       { 3 + }
2404132718Skan
2405169689Skan	     we stop before consuming the closing '}', even though we
2406132718Skan	     have not yet reached a `;'.  */
2407132718Skan	  if (nesting_depth == 0)
2408169689Skan	    return;
2409169689Skan
2410169689Skan	  /* If it is the closing '}' for a block that we have
2411132718Skan	     scanned, stop -- but only after consuming the token.
2412132718Skan	     That way given:
2413132718Skan
2414169689Skan		void f g () { ... }
2415132718Skan		typedef int I;
2416132718Skan
2417132718Skan	     we will stop after the body of the erroneously declared
2418132718Skan	     function, but before consuming the following `typedef'
2419132718Skan	     declaration.  */
2420132718Skan	  if (--nesting_depth == 0)
2421132718Skan	    {
2422132718Skan	      cp_lexer_consume_token (parser->lexer);
2423169689Skan	      return;
2424132718Skan	    }
2425169689Skan
2426169689Skan	case CPP_OPEN_BRACE:
2427169689Skan	  ++nesting_depth;
2428169689Skan	  break;
2429169689Skan
2430169689Skan	default:
2431169689Skan	  break;
2432132718Skan	}
2433169689Skan
2434132718Skan      /* Consume the token.  */
2435132718Skan      cp_lexer_consume_token (parser->lexer);
2436132718Skan    }
2437132718Skan}
2438132718Skan
2439132718Skan/* This function is called at the end of a statement or declaration.
2440132718Skan   If the next token is a semicolon, it is consumed; otherwise, error
2441132718Skan   recovery is attempted.  */
2442132718Skan
2443132718Skanstatic void
2444132718Skancp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2445132718Skan{
2446132718Skan  /* Look for the trailing `;'.  */
2447132718Skan  if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2448132718Skan    {
2449132718Skan      /* If there is additional (erroneous) input, skip to the end of
2450132718Skan	 the statement.  */
2451132718Skan      cp_parser_skip_to_end_of_statement (parser);
2452132718Skan      /* If the next token is now a `;', consume it.  */
2453132718Skan      if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2454132718Skan	cp_lexer_consume_token (parser->lexer);
2455132718Skan    }
2456132718Skan}
2457132718Skan
2458132718Skan/* Skip tokens until we have consumed an entire block, or until we
2459132718Skan   have consumed a non-nested `;'.  */
2460132718Skan
2461132718Skanstatic void
2462132718Skancp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2463132718Skan{
2464169689Skan  int nesting_depth = 0;
2465132718Skan
2466169689Skan  while (nesting_depth >= 0)
2467132718Skan    {
2468169689Skan      cp_token *token = cp_lexer_peek_token (parser->lexer);
2469132718Skan
2470169689Skan      switch (token->type)
2471132718Skan	{
2472169689Skan	case CPP_EOF:
2473169689Skan	case CPP_PRAGMA_EOL:
2474169689Skan	  /* If we've run out of tokens, stop.  */
2475169689Skan	  return;
2476169689Skan
2477169689Skan	case CPP_SEMICOLON:
2478169689Skan	  /* Stop if this is an unnested ';'. */
2479169689Skan	  if (!nesting_depth)
2480169689Skan	    nesting_depth = -1;
2481132718Skan	  break;
2482169689Skan
2483169689Skan	case CPP_CLOSE_BRACE:
2484169689Skan	  /* Stop if this is an unnested '}', or closes the outermost
2485169689Skan	     nesting level.  */
2486169689Skan	  nesting_depth--;
2487169689Skan	  if (!nesting_depth)
2488169689Skan	    nesting_depth = -1;
2489169689Skan	  break;
2490169689Skan
2491169689Skan	case CPP_OPEN_BRACE:
2492169689Skan	  /* Nest. */
2493169689Skan	  nesting_depth++;
2494169689Skan	  break;
2495169689Skan
2496169689Skan	default:
2497169689Skan	  break;
2498132718Skan	}
2499169689Skan
2500132718Skan      /* Consume the token.  */
2501169689Skan      cp_lexer_consume_token (parser->lexer);
2502132718Skan    }
2503132718Skan}
2504132718Skan
2505132718Skan/* Skip tokens until a non-nested closing curly brace is the next
2506132718Skan   token.  */
2507132718Skan
2508132718Skanstatic void
2509132718Skancp_parser_skip_to_closing_brace (cp_parser *parser)
2510132718Skan{
2511132718Skan  unsigned nesting_depth = 0;
2512132718Skan
2513132718Skan  while (true)
2514132718Skan    {
2515169689Skan      cp_token *token = cp_lexer_peek_token (parser->lexer);
2516132718Skan
2517169689Skan      switch (token->type)
2518169689Skan	{
2519169689Skan	case CPP_EOF:
2520169689Skan	case CPP_PRAGMA_EOL:
2521169689Skan	  /* If we've run out of tokens, stop.  */
2522169689Skan	  return;
2523169689Skan
2524169689Skan	case CPP_CLOSE_BRACE:
2525169689Skan	  /* If the next token is a non-nested `}', then we have reached
2526169689Skan	     the end of the current block.  */
2527169689Skan	  if (nesting_depth-- == 0)
2528169689Skan	    return;
2529169689Skan	  break;
2530169689Skan
2531169689Skan	case CPP_OPEN_BRACE:
2532169689Skan	  /* If it the next token is a `{', then we are entering a new
2533169689Skan	     block.  Consume the entire block.  */
2534169689Skan	  ++nesting_depth;
2535169689Skan	  break;
2536169689Skan
2537169689Skan	default:
2538169689Skan	  break;
2539169689Skan	}
2540169689Skan
2541132718Skan      /* Consume the token.  */
2542132718Skan      cp_lexer_consume_token (parser->lexer);
2543132718Skan    }
2544132718Skan}
2545132718Skan
2546169689Skan/* Consume tokens until we reach the end of the pragma.  The PRAGMA_TOK
2547169689Skan   parameter is the PRAGMA token, allowing us to purge the entire pragma
2548169689Skan   sequence.  */
2549169689Skan
2550169689Skanstatic void
2551169689Skancp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2552169689Skan{
2553169689Skan  cp_token *token;
2554169689Skan
2555169689Skan  parser->lexer->in_pragma = false;
2556169689Skan
2557169689Skan  do
2558169689Skan    token = cp_lexer_consume_token (parser->lexer);
2559169689Skan  while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2560169689Skan
2561169689Skan  /* Ensure that the pragma is not parsed again.  */
2562169689Skan  cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2563169689Skan}
2564169689Skan
2565169689Skan/* Require pragma end of line, resyncing with it as necessary.  The
2566169689Skan   arguments are as for cp_parser_skip_to_pragma_eol.  */
2567169689Skan
2568169689Skanstatic void
2569169689Skancp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2570169689Skan{
2571169689Skan  parser->lexer->in_pragma = false;
2572169689Skan  if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2573169689Skan    cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2574169689Skan}
2575169689Skan
2576169689Skan/* This is a simple wrapper around make_typename_type. When the id is
2577169689Skan   an unresolved identifier node, we can provide a superior diagnostic
2578169689Skan   using cp_parser_diagnose_invalid_type_name.  */
2579169689Skan
2580169689Skanstatic tree
2581169689Skancp_parser_make_typename_type (cp_parser *parser, tree scope, tree id)
2582169689Skan{
2583169689Skan  tree result;
2584169689Skan  if (TREE_CODE (id) == IDENTIFIER_NODE)
2585169689Skan    {
2586169689Skan      result = make_typename_type (scope, id, typename_type,
2587169689Skan				   /*complain=*/tf_none);
2588169689Skan      if (result == error_mark_node)
2589169689Skan	cp_parser_diagnose_invalid_type_name (parser, scope, id);
2590169689Skan      return result;
2591169689Skan    }
2592169689Skan  return make_typename_type (scope, id, typename_type, tf_error);
2593169689Skan}
2594169689Skan
2595169689Skan
2596132718Skan/* Create a new C++ parser.  */
2597132718Skan
2598132718Skanstatic cp_parser *
2599132718Skancp_parser_new (void)
2600132718Skan{
2601132718Skan  cp_parser *parser;
2602132718Skan  cp_lexer *lexer;
2603169689Skan  unsigned i;
2604132718Skan
2605132718Skan  /* cp_lexer_new_main is called before calling ggc_alloc because
2606132718Skan     cp_lexer_new_main might load a PCH file.  */
2607132718Skan  lexer = cp_lexer_new_main ();
2608132718Skan
2609169689Skan  /* Initialize the binops_by_token so that we can get the tree
2610169689Skan     directly from the token.  */
2611169689Skan  for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2612169689Skan    binops_by_token[binops[i].token_type] = binops[i];
2613169689Skan
2614169689Skan  parser = GGC_CNEW (cp_parser);
2615132718Skan  parser->lexer = lexer;
2616132718Skan  parser->context = cp_parser_context_new (NULL);
2617132718Skan
2618132718Skan  /* For now, we always accept GNU extensions.  */
2619132718Skan  parser->allow_gnu_extensions_p = 1;
2620132718Skan
2621132718Skan  /* The `>' token is a greater-than operator, not the end of a
2622132718Skan     template-id.  */
2623132718Skan  parser->greater_than_is_operator_p = true;
2624132718Skan
2625132718Skan  parser->default_arg_ok_p = true;
2626169689Skan
2627132718Skan  /* We are not parsing a constant-expression.  */
2628132718Skan  parser->integral_constant_expression_p = false;
2629132718Skan  parser->allow_non_integral_constant_expression_p = false;
2630132718Skan  parser->non_integral_constant_expression_p = false;
2631132718Skan
2632132718Skan  /* Local variable names are not forbidden.  */
2633132718Skan  parser->local_variables_forbidden_p = false;
2634132718Skan
2635132718Skan  /* We are not processing an `extern "C"' declaration.  */
2636132718Skan  parser->in_unbraced_linkage_specification_p = false;
2637132718Skan
2638132718Skan  /* We are not processing a declarator.  */
2639132718Skan  parser->in_declarator_p = false;
2640132718Skan
2641132718Skan  /* We are not processing a template-argument-list.  */
2642132718Skan  parser->in_template_argument_list_p = false;
2643132718Skan
2644132718Skan  /* We are not in an iteration statement.  */
2645169689Skan  parser->in_statement = 0;
2646132718Skan
2647132718Skan  /* We are not in a switch statement.  */
2648132718Skan  parser->in_switch_statement_p = false;
2649132718Skan
2650132718Skan  /* We are not parsing a type-id inside an expression.  */
2651132718Skan  parser->in_type_id_in_expr_p = false;
2652132718Skan
2653169689Skan  /* Declarations aren't implicitly extern "C".  */
2654169689Skan  parser->implicit_extern_c = false;
2655169689Skan
2656169689Skan  /* String literals should be translated to the execution character set.  */
2657169689Skan  parser->translate_strings_p = true;
2658169689Skan
2659169689Skan  /* We are not parsing a function body.  */
2660169689Skan  parser->in_function_body = false;
2661169689Skan
2662132718Skan  /* The unparsed function queue is empty.  */
2663132718Skan  parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2664132718Skan
2665132718Skan  /* There are no classes being defined.  */
2666132718Skan  parser->num_classes_being_defined = 0;
2667132718Skan
2668132718Skan  /* No template parameters apply.  */
2669132718Skan  parser->num_template_parameter_lists = 0;
2670132718Skan
2671132718Skan  return parser;
2672132718Skan}
2673132718Skan
2674169689Skan/* Create a cp_lexer structure which will emit the tokens in CACHE
2675169689Skan   and push it onto the parser's lexer stack.  This is used for delayed
2676169689Skan   parsing of in-class method bodies and default arguments, and should
2677169689Skan   not be confused with tentative parsing.  */
2678169689Skanstatic void
2679169689Skancp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2680169689Skan{
2681169689Skan  cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2682169689Skan  lexer->next = parser->lexer;
2683169689Skan  parser->lexer = lexer;
2684169689Skan
2685169689Skan  /* Move the current source position to that of the first token in the
2686169689Skan     new lexer.  */
2687169689Skan  cp_lexer_set_source_position_from_token (lexer->next_token);
2688169689Skan}
2689169689Skan
2690169689Skan/* Pop the top lexer off the parser stack.  This is never used for the
2691169689Skan   "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens.  */
2692169689Skanstatic void
2693169689Skancp_parser_pop_lexer (cp_parser *parser)
2694169689Skan{
2695169689Skan  cp_lexer *lexer = parser->lexer;
2696169689Skan  parser->lexer = lexer->next;
2697169689Skan  cp_lexer_destroy (lexer);
2698169689Skan
2699169689Skan  /* Put the current source position back where it was before this
2700169689Skan     lexer was pushed.  */
2701169689Skan  cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2702169689Skan}
2703169689Skan
2704132718Skan/* Lexical conventions [gram.lex]  */
2705132718Skan
2706132718Skan/* Parse an identifier.  Returns an IDENTIFIER_NODE representing the
2707132718Skan   identifier.  */
2708132718Skan
2709169689Skanstatic tree
2710132718Skancp_parser_identifier (cp_parser* parser)
2711132718Skan{
2712132718Skan  cp_token *token;
2713132718Skan
2714132718Skan  /* Look for the identifier.  */
2715132718Skan  token = cp_parser_require (parser, CPP_NAME, "identifier");
2716132718Skan  /* Return the value.  */
2717169689Skan  return token ? token->u.value : error_mark_node;
2718132718Skan}
2719132718Skan
2720169689Skan/* Parse a sequence of adjacent string constants.  Returns a
2721169689Skan   TREE_STRING representing the combined, nul-terminated string
2722169689Skan   constant.  If TRANSLATE is true, translate the string to the
2723169689Skan   execution character set.  If WIDE_OK is true, a wide string is
2724169689Skan   invalid here.
2725169689Skan
2726169689Skan   C++98 [lex.string] says that if a narrow string literal token is
2727169689Skan   adjacent to a wide string literal token, the behavior is undefined.
2728169689Skan   However, C99 6.4.5p4 says that this results in a wide string literal.
2729169689Skan   We follow C99 here, for consistency with the C front end.
2730169689Skan
2731169689Skan   This code is largely lifted from lex_string() in c-lex.c.
2732169689Skan
2733169689Skan   FUTURE: ObjC++ will need to handle @-strings here.  */
2734169689Skanstatic tree
2735169689Skancp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2736169689Skan{
2737169689Skan  tree value;
2738169689Skan  bool wide = false;
2739169689Skan  size_t count;
2740169689Skan  struct obstack str_ob;
2741169689Skan  cpp_string str, istr, *strs;
2742169689Skan  cp_token *tok;
2743169689Skan
2744169689Skan  tok = cp_lexer_peek_token (parser->lexer);
2745169689Skan  if (!cp_parser_is_string_literal (tok))
2746169689Skan    {
2747169689Skan      cp_parser_error (parser, "expected string-literal");
2748169689Skan      return error_mark_node;
2749169689Skan    }
2750169689Skan
2751169689Skan  /* Try to avoid the overhead of creating and destroying an obstack
2752169689Skan     for the common case of just one string.  */
2753169689Skan  if (!cp_parser_is_string_literal
2754169689Skan      (cp_lexer_peek_nth_token (parser->lexer, 2)))
2755169689Skan    {
2756169689Skan      cp_lexer_consume_token (parser->lexer);
2757169689Skan
2758169689Skan      str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2759169689Skan      str.len = TREE_STRING_LENGTH (tok->u.value);
2760169689Skan      count = 1;
2761169689Skan      if (tok->type == CPP_WSTRING)
2762169689Skan	wide = true;
2763169689Skan
2764169689Skan      strs = &str;
2765169689Skan    }
2766169689Skan  else
2767169689Skan    {
2768169689Skan      gcc_obstack_init (&str_ob);
2769169689Skan      count = 0;
2770169689Skan
2771169689Skan      do
2772169689Skan	{
2773169689Skan	  cp_lexer_consume_token (parser->lexer);
2774169689Skan	  count++;
2775169689Skan	  str.text = (unsigned char *)TREE_STRING_POINTER (tok->u.value);
2776169689Skan	  str.len = TREE_STRING_LENGTH (tok->u.value);
2777169689Skan	  if (tok->type == CPP_WSTRING)
2778169689Skan	    wide = true;
2779169689Skan
2780169689Skan	  obstack_grow (&str_ob, &str, sizeof (cpp_string));
2781169689Skan
2782169689Skan	  tok = cp_lexer_peek_token (parser->lexer);
2783169689Skan	}
2784169689Skan      while (cp_parser_is_string_literal (tok));
2785169689Skan
2786169689Skan      strs = (cpp_string *) obstack_finish (&str_ob);
2787169689Skan    }
2788169689Skan
2789169689Skan  if (wide && !wide_ok)
2790169689Skan    {
2791169689Skan      cp_parser_error (parser, "a wide string is invalid in this context");
2792169689Skan      wide = false;
2793169689Skan    }
2794169689Skan
2795169689Skan  if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2796169689Skan      (parse_in, strs, count, &istr, wide))
2797169689Skan    {
2798169689Skan      value = build_string (istr.len, (char *)istr.text);
2799169689Skan      free ((void *)istr.text);
2800169689Skan
2801169689Skan      TREE_TYPE (value) = wide ? wchar_array_type_node : char_array_type_node;
2802169689Skan      value = fix_string_type (value);
2803169689Skan    }
2804169689Skan  else
2805169689Skan    /* cpp_interpret_string has issued an error.  */
2806169689Skan    value = error_mark_node;
2807169689Skan
2808169689Skan  if (count > 1)
2809169689Skan    obstack_free (&str_ob, 0);
2810169689Skan
2811169689Skan  return value;
2812169689Skan}
2813169689Skan
2814169689Skan
2815132718Skan/* Basic concepts [gram.basic]  */
2816132718Skan
2817132718Skan/* Parse a translation-unit.
2818132718Skan
2819132718Skan   translation-unit:
2820169689Skan     declaration-seq [opt]
2821132718Skan
2822132718Skan   Returns TRUE if all went well.  */
2823132718Skan
2824132718Skanstatic bool
2825132718Skancp_parser_translation_unit (cp_parser* parser)
2826132718Skan{
2827169689Skan  /* The address of the first non-permanent object on the declarator
2828169689Skan     obstack.  */
2829169689Skan  static void *declarator_obstack_base;
2830169689Skan
2831169689Skan  bool success;
2832169689Skan
2833169689Skan  /* Create the declarator obstack, if necessary.  */
2834169689Skan  if (!cp_error_declarator)
2835132718Skan    {
2836169689Skan      gcc_obstack_init (&declarator_obstack);
2837169689Skan      /* Create the error declarator.  */
2838169689Skan      cp_error_declarator = make_declarator (cdk_error);
2839169689Skan      /* Create the empty parameter list.  */
2840169689Skan      no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
2841169689Skan      /* Remember where the base of the declarator obstack lies.  */
2842169689Skan      declarator_obstack_base = obstack_next_free (&declarator_obstack);
2843169689Skan    }
2844132718Skan
2845169689Skan  cp_parser_declaration_seq_opt (parser);
2846169689Skan
2847169689Skan  /* If there are no tokens left then all went well.  */
2848169689Skan  if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2849169689Skan    {
2850169689Skan      /* Get rid of the token array; we don't need it any more.  */
2851169689Skan      cp_lexer_destroy (parser->lexer);
2852169689Skan      parser->lexer = NULL;
2853169689Skan
2854169689Skan      /* This file might have been a context that's implicitly extern
2855169689Skan	 "C".  If so, pop the lang context.  (Only relevant for PCH.) */
2856169689Skan      if (parser->implicit_extern_c)
2857169689Skan	{
2858169689Skan	  pop_lang_context ();
2859169689Skan	  parser->implicit_extern_c = false;
2860169689Skan	}
2861169689Skan
2862169689Skan      /* Finish up.  */
2863169689Skan      finish_translation_unit ();
2864169689Skan
2865169689Skan      success = true;
2866169689Skan    }
2867169689Skan  else
2868169689Skan    {
2869132718Skan      cp_parser_error (parser, "expected declaration");
2870169689Skan      success = false;
2871132718Skan    }
2872132718Skan
2873169689Skan  /* Make sure the declarator obstack was fully cleaned up.  */
2874169689Skan  gcc_assert (obstack_next_free (&declarator_obstack)
2875169689Skan	      == declarator_obstack_base);
2876132718Skan
2877132718Skan  /* All went well.  */
2878169689Skan  return success;
2879132718Skan}
2880132718Skan
2881132718Skan/* Expressions [gram.expr] */
2882132718Skan
2883132718Skan/* Parse a primary-expression.
2884132718Skan
2885132718Skan   primary-expression:
2886132718Skan     literal
2887132718Skan     this
2888132718Skan     ( expression )
2889132718Skan     id-expression
2890132718Skan
2891132718Skan   GNU Extensions:
2892132718Skan
2893132718Skan   primary-expression:
2894132718Skan     ( compound-statement )
2895132718Skan     __builtin_va_arg ( assignment-expression , type-id )
2896169689Skan     __builtin_offsetof ( type-id , offsetof-expression )
2897132718Skan
2898169689Skan   Objective-C++ Extension:
2899169689Skan
2900169689Skan   primary-expression:
2901169689Skan     objc-expression
2902169689Skan
2903132718Skan   literal:
2904132718Skan     __null
2905132718Skan
2906169689Skan   ADDRESS_P is true iff this expression was immediately preceded by
2907169689Skan   "&" and therefore might denote a pointer-to-member.  CAST_P is true
2908169689Skan   iff this expression is the target of a cast.  TEMPLATE_ARG_P is
2909169689Skan   true iff this expression is a template argument.
2910132718Skan
2911169689Skan   Returns a representation of the expression.  Upon return, *IDK
2912169689Skan   indicates what kind of id-expression (if any) was present.  */
2913132718Skan
2914132718Skanstatic tree
2915169689Skancp_parser_primary_expression (cp_parser *parser,
2916169689Skan			      bool address_p,
2917169689Skan			      bool cast_p,
2918169689Skan			      bool template_arg_p,
2919169689Skan			      cp_id_kind *idk)
2920132718Skan{
2921132718Skan  cp_token *token;
2922132718Skan
2923132718Skan  /* Assume the primary expression is not an id-expression.  */
2924132718Skan  *idk = CP_ID_KIND_NONE;
2925132718Skan
2926132718Skan  /* Peek at the next token.  */
2927132718Skan  token = cp_lexer_peek_token (parser->lexer);
2928132718Skan  switch (token->type)
2929132718Skan    {
2930132718Skan      /* literal:
2931132718Skan	   integer-literal
2932132718Skan	   character-literal
2933132718Skan	   floating-literal
2934132718Skan	   string-literal
2935132718Skan	   boolean-literal  */
2936132718Skan    case CPP_CHAR:
2937132718Skan    case CPP_WCHAR:
2938132718Skan    case CPP_NUMBER:
2939132718Skan      token = cp_lexer_consume_token (parser->lexer);
2940169689Skan      /* Floating-point literals are only allowed in an integral
2941169689Skan	 constant expression if they are cast to an integral or
2942169689Skan	 enumeration type.  */
2943169689Skan      if (TREE_CODE (token->u.value) == REAL_CST
2944169689Skan	  && parser->integral_constant_expression_p
2945169689Skan	  && pedantic)
2946169689Skan	{
2947169689Skan	  /* CAST_P will be set even in invalid code like "int(2.7 +
2948169689Skan	     ...)".   Therefore, we have to check that the next token
2949169689Skan	     is sure to end the cast.  */
2950169689Skan	  if (cast_p)
2951169689Skan	    {
2952169689Skan	      cp_token *next_token;
2953132718Skan
2954169689Skan	      next_token = cp_lexer_peek_token (parser->lexer);
2955169689Skan	      if (/* The comma at the end of an
2956169689Skan		     enumerator-definition.  */
2957169689Skan		  next_token->type != CPP_COMMA
2958169689Skan		  /* The curly brace at the end of an enum-specifier.  */
2959169689Skan		  && next_token->type != CPP_CLOSE_BRACE
2960169689Skan		  /* The end of a statement.  */
2961169689Skan		  && next_token->type != CPP_SEMICOLON
2962169689Skan		  /* The end of the cast-expression.  */
2963169689Skan		  && next_token->type != CPP_CLOSE_PAREN
2964169689Skan		  /* The end of an array bound.  */
2965169689Skan		  && next_token->type != CPP_CLOSE_SQUARE
2966169689Skan		  /* The closing ">" in a template-argument-list.  */
2967169689Skan		  && (next_token->type != CPP_GREATER
2968169689Skan		      || parser->greater_than_is_operator_p))
2969169689Skan		cast_p = false;
2970169689Skan	    }
2971169689Skan
2972169689Skan	  /* If we are within a cast, then the constraint that the
2973169689Skan	     cast is to an integral or enumeration type will be
2974169689Skan	     checked at that point.  If we are not within a cast, then
2975169689Skan	     this code is invalid.  */
2976169689Skan	  if (!cast_p)
2977169689Skan	    cp_parser_non_integral_constant_expression
2978169689Skan	      (parser, "floating-point literal");
2979169689Skan	}
2980169689Skan      return token->u.value;
2981169689Skan
2982169689Skan    case CPP_STRING:
2983169689Skan    case CPP_WSTRING:
2984169689Skan      /* ??? Should wide strings be allowed when parser->translate_strings_p
2985169689Skan	 is false (i.e. in attributes)?  If not, we can kill the third
2986169689Skan	 argument to cp_parser_string_literal.  */
2987169689Skan      return cp_parser_string_literal (parser,
2988169689Skan				       parser->translate_strings_p,
2989169689Skan				       true);
2990169689Skan
2991132718Skan    case CPP_OPEN_PAREN:
2992132718Skan      {
2993132718Skan	tree expr;
2994132718Skan	bool saved_greater_than_is_operator_p;
2995132718Skan
2996132718Skan	/* Consume the `('.  */
2997132718Skan	cp_lexer_consume_token (parser->lexer);
2998132718Skan	/* Within a parenthesized expression, a `>' token is always
2999132718Skan	   the greater-than operator.  */
3000169689Skan	saved_greater_than_is_operator_p
3001132718Skan	  = parser->greater_than_is_operator_p;
3002132718Skan	parser->greater_than_is_operator_p = true;
3003132718Skan	/* If we see `( { ' then we are looking at the beginning of
3004132718Skan	   a GNU statement-expression.  */
3005132718Skan	if (cp_parser_allow_gnu_extensions_p (parser)
3006132718Skan	    && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3007132718Skan	  {
3008132718Skan	    /* Statement-expressions are not allowed by the standard.  */
3009132718Skan	    if (pedantic)
3010169689Skan	      pedwarn ("ISO C++ forbids braced-groups within expressions");
3011169689Skan
3012132718Skan	    /* And they're not allowed outside of a function-body; you
3013132718Skan	       cannot, for example, write:
3014169689Skan
3015169689Skan		 int i = ({ int j = 3; j + 1; });
3016169689Skan
3017132718Skan	       at class or namespace scope.  */
3018169689Skan	    if (!parser->in_function_body)
3019132718Skan	      error ("statement-expressions are allowed only inside functions");
3020132718Skan	    /* Start the statement-expression.  */
3021132718Skan	    expr = begin_stmt_expr ();
3022132718Skan	    /* Parse the compound-statement.  */
3023169689Skan	    cp_parser_compound_statement (parser, expr, false);
3024132718Skan	    /* Finish up.  */
3025132718Skan	    expr = finish_stmt_expr (expr, false);
3026132718Skan	  }
3027132718Skan	else
3028132718Skan	  {
3029132718Skan	    /* Parse the parenthesized expression.  */
3030169689Skan	    expr = cp_parser_expression (parser, cast_p);
3031132718Skan	    /* Let the front end know that this expression was
3032132718Skan	       enclosed in parentheses. This matters in case, for
3033132718Skan	       example, the expression is of the form `A::B', since
3034132718Skan	       `&A::B' might be a pointer-to-member, but `&(A::B)' is
3035132718Skan	       not.  */
3036132718Skan	    finish_parenthesized_expr (expr);
3037132718Skan	  }
3038132718Skan	/* The `>' token might be the end of a template-id or
3039132718Skan	   template-parameter-list now.  */
3040169689Skan	parser->greater_than_is_operator_p
3041132718Skan	  = saved_greater_than_is_operator_p;
3042132718Skan	/* Consume the `)'.  */
3043132718Skan	if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
3044132718Skan	  cp_parser_skip_to_end_of_statement (parser);
3045132718Skan
3046132718Skan	return expr;
3047132718Skan      }
3048132718Skan
3049132718Skan    case CPP_KEYWORD:
3050132718Skan      switch (token->keyword)
3051132718Skan	{
3052132718Skan	  /* These two are the boolean literals.  */
3053132718Skan	case RID_TRUE:
3054132718Skan	  cp_lexer_consume_token (parser->lexer);
3055132718Skan	  return boolean_true_node;
3056132718Skan	case RID_FALSE:
3057132718Skan	  cp_lexer_consume_token (parser->lexer);
3058132718Skan	  return boolean_false_node;
3059169689Skan
3060132718Skan	  /* The `__null' literal.  */
3061132718Skan	case RID_NULL:
3062132718Skan	  cp_lexer_consume_token (parser->lexer);
3063132718Skan	  return null_node;
3064132718Skan
3065132718Skan	  /* Recognize the `this' keyword.  */
3066132718Skan	case RID_THIS:
3067132718Skan	  cp_lexer_consume_token (parser->lexer);
3068132718Skan	  if (parser->local_variables_forbidden_p)
3069132718Skan	    {
3070169689Skan	      error ("%<this%> may not be used in this context");
3071132718Skan	      return error_mark_node;
3072132718Skan	    }
3073132718Skan	  /* Pointers cannot appear in constant-expressions.  */
3074132718Skan	  if (cp_parser_non_integral_constant_expression (parser,
3075132718Skan							  "`this'"))
3076132718Skan	    return error_mark_node;
3077132718Skan	  return finish_this_expr ();
3078132718Skan
3079132718Skan	  /* The `operator' keyword can be the beginning of an
3080132718Skan	     id-expression.  */
3081132718Skan	case RID_OPERATOR:
3082132718Skan	  goto id_expression;
3083132718Skan
3084132718Skan	case RID_FUNCTION_NAME:
3085132718Skan	case RID_PRETTY_FUNCTION_NAME:
3086132718Skan	case RID_C99_FUNCTION_NAME:
3087132718Skan	  /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3088132718Skan	     __func__ are the names of variables -- but they are
3089132718Skan	     treated specially.  Therefore, they are handled here,
3090132718Skan	     rather than relying on the generic id-expression logic
3091169689Skan	     below.  Grammatically, these names are id-expressions.
3092132718Skan
3093132718Skan	     Consume the token.  */
3094132718Skan	  token = cp_lexer_consume_token (parser->lexer);
3095132718Skan	  /* Look up the name.  */
3096169689Skan	  return finish_fname (token->u.value);
3097132718Skan
3098132718Skan	case RID_VA_ARG:
3099132718Skan	  {
3100132718Skan	    tree expression;
3101132718Skan	    tree type;
3102132718Skan
3103132718Skan	    /* The `__builtin_va_arg' construct is used to handle
3104132718Skan	       `va_arg'.  Consume the `__builtin_va_arg' token.  */
3105132718Skan	    cp_lexer_consume_token (parser->lexer);
3106132718Skan	    /* Look for the opening `('.  */
3107132718Skan	    cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3108132718Skan	    /* Now, parse the assignment-expression.  */
3109169689Skan	    expression = cp_parser_assignment_expression (parser,
3110169689Skan							  /*cast_p=*/false);
3111132718Skan	    /* Look for the `,'.  */
3112132718Skan	    cp_parser_require (parser, CPP_COMMA, "`,'");
3113132718Skan	    /* Parse the type-id.  */
3114132718Skan	    type = cp_parser_type_id (parser);
3115132718Skan	    /* Look for the closing `)'.  */
3116132718Skan	    cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3117132718Skan	    /* Using `va_arg' in a constant-expression is not
3118132718Skan	       allowed.  */
3119132718Skan	    if (cp_parser_non_integral_constant_expression (parser,
3120132718Skan							    "`va_arg'"))
3121132718Skan	      return error_mark_node;
3122132718Skan	    return build_x_va_arg (expression, type);
3123132718Skan	  }
3124132718Skan
3125132718Skan	case RID_OFFSETOF:
3126169689Skan	  return cp_parser_builtin_offsetof (parser);
3127132718Skan
3128169689Skan	  /* Objective-C++ expressions.  */
3129169689Skan	case RID_AT_ENCODE:
3130169689Skan	case RID_AT_PROTOCOL:
3131169689Skan	case RID_AT_SELECTOR:
3132169689Skan	  return cp_parser_objc_expression (parser);
3133132718Skan
3134132718Skan	default:
3135132718Skan	  cp_parser_error (parser, "expected primary-expression");
3136132718Skan	  return error_mark_node;
3137132718Skan	}
3138132718Skan
3139132718Skan      /* An id-expression can start with either an identifier, a
3140132718Skan	 `::' as the beginning of a qualified-id, or the "operator"
3141132718Skan	 keyword.  */
3142132718Skan    case CPP_NAME:
3143132718Skan    case CPP_SCOPE:
3144132718Skan    case CPP_TEMPLATE_ID:
3145132718Skan    case CPP_NESTED_NAME_SPECIFIER:
3146132718Skan      {
3147132718Skan	tree id_expression;
3148132718Skan	tree decl;
3149132718Skan	const char *error_msg;
3150169689Skan	bool template_p;
3151169689Skan	bool done;
3152132718Skan
3153132718Skan      id_expression:
3154132718Skan	/* Parse the id-expression.  */
3155169689Skan	id_expression
3156169689Skan	  = cp_parser_id_expression (parser,
3157132718Skan				     /*template_keyword_p=*/false,
3158132718Skan				     /*check_dependency_p=*/true,
3159169689Skan				     &template_p,
3160169689Skan				     /*declarator_p=*/false,
3161169689Skan				     /*optional_p=*/false);
3162132718Skan	if (id_expression == error_mark_node)
3163132718Skan	  return error_mark_node;
3164169689Skan	token = cp_lexer_peek_token (parser->lexer);
3165169689Skan	done = (token->type != CPP_OPEN_SQUARE
3166169689Skan		&& token->type != CPP_OPEN_PAREN
3167169689Skan		&& token->type != CPP_DOT
3168169689Skan		&& token->type != CPP_DEREF
3169169689Skan		&& token->type != CPP_PLUS_PLUS
3170169689Skan		&& token->type != CPP_MINUS_MINUS);
3171132718Skan	/* If we have a template-id, then no further lookup is
3172132718Skan	   required.  If the template-id was for a template-class, we
3173132718Skan	   will sometimes have a TYPE_DECL at this point.  */
3174169689Skan	if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3175169689Skan		 || TREE_CODE (id_expression) == TYPE_DECL)
3176132718Skan	  decl = id_expression;
3177132718Skan	/* Look up the name.  */
3178169689Skan	else
3179132718Skan	  {
3180169689Skan	    tree ambiguous_decls;
3181169689Skan
3182169689Skan	    decl = cp_parser_lookup_name (parser, id_expression,
3183169689Skan					  none_type,
3184169689Skan					  template_p,
3185169689Skan					  /*is_namespace=*/false,
3186169689Skan					  /*check_dependency=*/true,
3187169689Skan					  &ambiguous_decls);
3188169689Skan	    /* If the lookup was ambiguous, an error will already have
3189169689Skan	       been issued.  */
3190169689Skan	    if (ambiguous_decls)
3191169689Skan	      return error_mark_node;
3192169689Skan
3193169689Skan	    /* In Objective-C++, an instance variable (ivar) may be preferred
3194169689Skan	       to whatever cp_parser_lookup_name() found.  */
3195169689Skan	    decl = objc_lookup_ivar (decl, id_expression);
3196169689Skan
3197132718Skan	    /* If name lookup gives us a SCOPE_REF, then the
3198169689Skan	       qualifying scope was dependent.  */
3199132718Skan	    if (TREE_CODE (decl) == SCOPE_REF)
3200171825Skan	      {
3201171825Skan		/* At this point, we do not know if DECL is a valid
3202171825Skan		   integral constant expression.  We assume that it is
3203171825Skan		   in fact such an expression, so that code like:
3204171825Skan
3205171825Skan		      template <int N> struct A {
3206171825Skan			int a[B<N>::i];
3207171825Skan		      };
3208171825Skan
3209171825Skan		   is accepted.  At template-instantiation time, we
3210171825Skan		   will check that B<N>::i is actually a constant.  */
3211171825Skan		return decl;
3212171825Skan	      }
3213132718Skan	    /* Check to see if DECL is a local variable in a context
3214132718Skan	       where that is forbidden.  */
3215132718Skan	    if (parser->local_variables_forbidden_p
3216132718Skan		&& local_variable_p (decl))
3217132718Skan	      {
3218132718Skan		/* It might be that we only found DECL because we are
3219132718Skan		   trying to be generous with pre-ISO scoping rules.
3220132718Skan		   For example, consider:
3221132718Skan
3222132718Skan		     int i;
3223132718Skan		     void g() {
3224132718Skan		       for (int i = 0; i < 10; ++i) {}
3225132718Skan		       extern void f(int j = i);
3226132718Skan		     }
3227132718Skan
3228169689Skan		   Here, name look up will originally find the out
3229132718Skan		   of scope `i'.  We need to issue a warning message,
3230132718Skan		   but then use the global `i'.  */
3231132718Skan		decl = check_for_out_of_scope_variable (decl);
3232132718Skan		if (local_variable_p (decl))
3233132718Skan		  {
3234169689Skan		    error ("local variable %qD may not appear in this context",
3235132718Skan			   decl);
3236132718Skan		    return error_mark_node;
3237132718Skan		  }
3238132718Skan	      }
3239132718Skan	  }
3240169689Skan
3241169689Skan	decl = (finish_id_expression
3242169689Skan		(id_expression, decl, parser->scope,
3243169689Skan		 idk,
3244169689Skan		 parser->integral_constant_expression_p,
3245169689Skan		 parser->allow_non_integral_constant_expression_p,
3246169689Skan		 &parser->non_integral_constant_expression_p,
3247169689Skan		 template_p, done, address_p,
3248169689Skan		 template_arg_p,
3249169689Skan		 &error_msg));
3250132718Skan	if (error_msg)
3251132718Skan	  cp_parser_error (parser, error_msg);
3252132718Skan	return decl;
3253132718Skan      }
3254132718Skan
3255132718Skan      /* Anything else is an error.  */
3256132718Skan    default:
3257169689Skan      /* ...unless we have an Objective-C++ message or string literal, that is.  */
3258169689Skan      if (c_dialect_objc ()
3259169689Skan	  && (token->type == CPP_OPEN_SQUARE || token->type == CPP_OBJC_STRING))
3260169689Skan	return cp_parser_objc_expression (parser);
3261169689Skan
3262132718Skan      cp_parser_error (parser, "expected primary-expression");
3263132718Skan      return error_mark_node;
3264132718Skan    }
3265132718Skan}
3266132718Skan
3267132718Skan/* Parse an id-expression.
3268132718Skan
3269132718Skan   id-expression:
3270132718Skan     unqualified-id
3271132718Skan     qualified-id
3272132718Skan
3273132718Skan   qualified-id:
3274132718Skan     :: [opt] nested-name-specifier template [opt] unqualified-id
3275132718Skan     :: identifier
3276132718Skan     :: operator-function-id
3277132718Skan     :: template-id
3278132718Skan
3279132718Skan   Return a representation of the unqualified portion of the
3280132718Skan   identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
3281132718Skan   a `::' or nested-name-specifier.
3282132718Skan
3283132718Skan   Often, if the id-expression was a qualified-id, the caller will
3284132718Skan   want to make a SCOPE_REF to represent the qualified-id.  This
3285132718Skan   function does not do this in order to avoid wastefully creating
3286132718Skan   SCOPE_REFs when they are not required.
3287132718Skan
3288132718Skan   If TEMPLATE_KEYWORD_P is true, then we have just seen the
3289132718Skan   `template' keyword.
3290132718Skan
3291132718Skan   If CHECK_DEPENDENCY_P is false, then names are looked up inside
3292169689Skan   uninstantiated templates.
3293132718Skan
3294132718Skan   If *TEMPLATE_P is non-NULL, it is set to true iff the
3295132718Skan   `template' keyword is used to explicitly indicate that the entity
3296169689Skan   named is a template.
3297132718Skan
3298132718Skan   If DECLARATOR_P is true, the id-expression is appearing as part of
3299132718Skan   a declarator, rather than as part of an expression.  */
3300132718Skan
3301132718Skanstatic tree
3302132718Skancp_parser_id_expression (cp_parser *parser,
3303132718Skan			 bool template_keyword_p,
3304132718Skan			 bool check_dependency_p,
3305132718Skan			 bool *template_p,
3306169689Skan			 bool declarator_p,
3307169689Skan			 bool optional_p)
3308132718Skan{
3309132718Skan  bool global_scope_p;
3310132718Skan  bool nested_name_specifier_p;
3311132718Skan
3312132718Skan  /* Assume the `template' keyword was not used.  */
3313132718Skan  if (template_p)
3314169689Skan    *template_p = template_keyword_p;
3315132718Skan
3316132718Skan  /* Look for the optional `::' operator.  */
3317169689Skan  global_scope_p
3318169689Skan    = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3319132718Skan       != NULL_TREE);
3320132718Skan  /* Look for the optional nested-name-specifier.  */
3321169689Skan  nested_name_specifier_p
3322132718Skan    = (cp_parser_nested_name_specifier_opt (parser,
3323132718Skan					    /*typename_keyword_p=*/false,
3324132718Skan					    check_dependency_p,
3325132718Skan					    /*type_p=*/false,
3326146895Skan					    declarator_p)
3327132718Skan       != NULL_TREE);
3328132718Skan  /* If there is a nested-name-specifier, then we are looking at
3329132718Skan     the first qualified-id production.  */
3330132718Skan  if (nested_name_specifier_p)
3331132718Skan    {
3332132718Skan      tree saved_scope;
3333132718Skan      tree saved_object_scope;
3334132718Skan      tree saved_qualifying_scope;
3335132718Skan      tree unqualified_id;
3336132718Skan      bool is_template;
3337132718Skan
3338132718Skan      /* See if the next token is the `template' keyword.  */
3339132718Skan      if (!template_p)
3340132718Skan	template_p = &is_template;
3341132718Skan      *template_p = cp_parser_optional_template_keyword (parser);
3342132718Skan      /* Name lookup we do during the processing of the
3343132718Skan	 unqualified-id might obliterate SCOPE.  */
3344132718Skan      saved_scope = parser->scope;
3345132718Skan      saved_object_scope = parser->object_scope;
3346132718Skan      saved_qualifying_scope = parser->qualifying_scope;
3347132718Skan      /* Process the final unqualified-id.  */
3348132718Skan      unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3349132718Skan						 check_dependency_p,
3350169689Skan						 declarator_p,
3351169689Skan						 /*optional_p=*/false);
3352132718Skan      /* Restore the SAVED_SCOPE for our caller.  */
3353132718Skan      parser->scope = saved_scope;
3354132718Skan      parser->object_scope = saved_object_scope;
3355132718Skan      parser->qualifying_scope = saved_qualifying_scope;
3356132718Skan
3357132718Skan      return unqualified_id;
3358132718Skan    }
3359132718Skan  /* Otherwise, if we are in global scope, then we are looking at one
3360132718Skan     of the other qualified-id productions.  */
3361132718Skan  else if (global_scope_p)
3362132718Skan    {
3363132718Skan      cp_token *token;
3364132718Skan      tree id;
3365132718Skan
3366132718Skan      /* Peek at the next token.  */
3367132718Skan      token = cp_lexer_peek_token (parser->lexer);
3368132718Skan
3369132718Skan      /* If it's an identifier, and the next token is not a "<", then
3370132718Skan	 we can avoid the template-id case.  This is an optimization
3371132718Skan	 for this common case.  */
3372169689Skan      if (token->type == CPP_NAME
3373169689Skan	  && !cp_parser_nth_token_starts_template_argument_list_p
3374132718Skan	       (parser, 2))
3375132718Skan	return cp_parser_identifier (parser);
3376132718Skan
3377132718Skan      cp_parser_parse_tentatively (parser);
3378132718Skan      /* Try a template-id.  */
3379169689Skan      id = cp_parser_template_id (parser,
3380132718Skan				  /*template_keyword_p=*/false,
3381132718Skan				  /*check_dependency_p=*/true,
3382132718Skan				  declarator_p);
3383132718Skan      /* If that worked, we're done.  */
3384132718Skan      if (cp_parser_parse_definitely (parser))
3385132718Skan	return id;
3386132718Skan
3387132718Skan      /* Peek at the next token.  (Changes in the token buffer may
3388132718Skan	 have invalidated the pointer obtained above.)  */
3389132718Skan      token = cp_lexer_peek_token (parser->lexer);
3390132718Skan
3391132718Skan      switch (token->type)
3392132718Skan	{
3393132718Skan	case CPP_NAME:
3394132718Skan	  return cp_parser_identifier (parser);
3395132718Skan
3396132718Skan	case CPP_KEYWORD:
3397132718Skan	  if (token->keyword == RID_OPERATOR)
3398132718Skan	    return cp_parser_operator_function_id (parser);
3399132718Skan	  /* Fall through.  */
3400169689Skan
3401132718Skan	default:
3402132718Skan	  cp_parser_error (parser, "expected id-expression");
3403132718Skan	  return error_mark_node;
3404132718Skan	}
3405132718Skan    }
3406132718Skan  else
3407132718Skan    return cp_parser_unqualified_id (parser, template_keyword_p,
3408132718Skan				     /*check_dependency_p=*/true,
3409169689Skan				     declarator_p,
3410169689Skan				     optional_p);
3411132718Skan}
3412132718Skan
3413132718Skan/* Parse an unqualified-id.
3414132718Skan
3415132718Skan   unqualified-id:
3416132718Skan     identifier
3417132718Skan     operator-function-id
3418132718Skan     conversion-function-id
3419132718Skan     ~ class-name
3420132718Skan     template-id
3421132718Skan
3422132718Skan   If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3423132718Skan   keyword, in a construct like `A::template ...'.
3424132718Skan
3425132718Skan   Returns a representation of unqualified-id.  For the `identifier'
3426132718Skan   production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
3427132718Skan   production a BIT_NOT_EXPR is returned; the operand of the
3428132718Skan   BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
3429132718Skan   other productions, see the documentation accompanying the
3430132718Skan   corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
3431132718Skan   names are looked up in uninstantiated templates.  If DECLARATOR_P
3432132718Skan   is true, the unqualified-id is appearing as part of a declarator,
3433132718Skan   rather than as part of an expression.  */
3434132718Skan
3435132718Skanstatic tree
3436169689Skancp_parser_unqualified_id (cp_parser* parser,
3437169689Skan			  bool template_keyword_p,
3438132718Skan			  bool check_dependency_p,
3439169689Skan			  bool declarator_p,
3440169689Skan			  bool optional_p)
3441132718Skan{
3442132718Skan  cp_token *token;
3443132718Skan
3444132718Skan  /* Peek at the next token.  */
3445132718Skan  token = cp_lexer_peek_token (parser->lexer);
3446169689Skan
3447132718Skan  switch (token->type)
3448132718Skan    {
3449132718Skan    case CPP_NAME:
3450132718Skan      {
3451132718Skan	tree id;
3452132718Skan
3453132718Skan	/* We don't know yet whether or not this will be a
3454132718Skan	   template-id.  */
3455132718Skan	cp_parser_parse_tentatively (parser);
3456132718Skan	/* Try a template-id.  */
3457132718Skan	id = cp_parser_template_id (parser, template_keyword_p,
3458132718Skan				    check_dependency_p,
3459132718Skan				    declarator_p);
3460132718Skan	/* If it worked, we're done.  */
3461132718Skan	if (cp_parser_parse_definitely (parser))
3462132718Skan	  return id;
3463132718Skan	/* Otherwise, it's an ordinary identifier.  */
3464132718Skan	return cp_parser_identifier (parser);
3465132718Skan      }
3466132718Skan
3467132718Skan    case CPP_TEMPLATE_ID:
3468132718Skan      return cp_parser_template_id (parser, template_keyword_p,
3469132718Skan				    check_dependency_p,
3470132718Skan				    declarator_p);
3471132718Skan
3472132718Skan    case CPP_COMPL:
3473132718Skan      {
3474132718Skan	tree type_decl;
3475132718Skan	tree qualifying_scope;
3476132718Skan	tree object_scope;
3477132718Skan	tree scope;
3478146895Skan	bool done;
3479132718Skan
3480132718Skan	/* Consume the `~' token.  */
3481132718Skan	cp_lexer_consume_token (parser->lexer);
3482132718Skan	/* Parse the class-name.  The standard, as written, seems to
3483132718Skan	   say that:
3484132718Skan
3485132718Skan	     template <typename T> struct S { ~S (); };
3486132718Skan	     template <typename T> S<T>::~S() {}
3487132718Skan
3488169689Skan	   is invalid, since `~' must be followed by a class-name, but
3489132718Skan	   `S<T>' is dependent, and so not known to be a class.
3490132718Skan	   That's not right; we need to look in uninstantiated
3491132718Skan	   templates.  A further complication arises from:
3492132718Skan
3493132718Skan	     template <typename T> void f(T t) {
3494132718Skan	       t.T::~T();
3495169689Skan	     }
3496132718Skan
3497132718Skan	   Here, it is not possible to look up `T' in the scope of `T'
3498132718Skan	   itself.  We must look in both the current scope, and the
3499169689Skan	   scope of the containing complete expression.
3500132718Skan
3501132718Skan	   Yet another issue is:
3502132718Skan
3503169689Skan	     struct S {
3504169689Skan	       int S;
3505169689Skan	       ~S();
3506169689Skan	     };
3507132718Skan
3508169689Skan	     S::~S() {}
3509132718Skan
3510169689Skan	   The standard does not seem to say that the `S' in `~S'
3511132718Skan	   should refer to the type `S' and not the data member
3512132718Skan	   `S::S'.  */
3513132718Skan
3514132718Skan	/* DR 244 says that we look up the name after the "~" in the
3515132718Skan	   same scope as we looked up the qualifying name.  That idea
3516132718Skan	   isn't fully worked out; it's more complicated than that.  */
3517132718Skan	scope = parser->scope;
3518132718Skan	object_scope = parser->object_scope;
3519132718Skan	qualifying_scope = parser->qualifying_scope;
3520132718Skan
3521169689Skan	/* Check for invalid scopes.  */
3522169689Skan	if (scope == error_mark_node)
3523169689Skan	  {
3524169689Skan	    if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3525169689Skan	      cp_lexer_consume_token (parser->lexer);
3526169689Skan	    return error_mark_node;
3527169689Skan	  }
3528169689Skan	if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3529169689Skan	  {
3530169689Skan	    if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3531169689Skan	      error ("scope %qT before %<~%> is not a class-name", scope);
3532169689Skan	    cp_parser_simulate_error (parser);
3533169689Skan	    if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3534169689Skan	      cp_lexer_consume_token (parser->lexer);
3535169689Skan	    return error_mark_node;
3536169689Skan	  }
3537169689Skan	gcc_assert (!scope || TYPE_P (scope));
3538169689Skan
3539132718Skan	/* If the name is of the form "X::~X" it's OK.  */
3540169689Skan	token = cp_lexer_peek_token (parser->lexer);
3541169689Skan	if (scope
3542169689Skan	    && token->type == CPP_NAME
3543169689Skan	    && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3544132718Skan		== CPP_OPEN_PAREN)
3545169689Skan	    && constructor_name_p (token->u.value, scope))
3546132718Skan	  {
3547132718Skan	    cp_lexer_consume_token (parser->lexer);
3548132718Skan	    return build_nt (BIT_NOT_EXPR, scope);
3549132718Skan	  }
3550132718Skan
3551132718Skan	/* If there was an explicit qualification (S::~T), first look
3552132718Skan	   in the scope given by the qualification (i.e., S).  */
3553146895Skan	done = false;
3554146895Skan	type_decl = NULL_TREE;
3555132718Skan	if (scope)
3556132718Skan	  {
3557132718Skan	    cp_parser_parse_tentatively (parser);
3558169689Skan	    type_decl = cp_parser_class_name (parser,
3559132718Skan					      /*typename_keyword_p=*/false,
3560132718Skan					      /*template_keyword_p=*/false,
3561169689Skan					      none_type,
3562132718Skan					      /*check_dependency=*/false,
3563132718Skan					      /*class_head_p=*/false,
3564132718Skan					      declarator_p);
3565132718Skan	    if (cp_parser_parse_definitely (parser))
3566146895Skan	      done = true;
3567132718Skan	  }
3568132718Skan	/* In "N::S::~S", look in "N" as well.  */
3569146895Skan	if (!done && scope && qualifying_scope)
3570132718Skan	  {
3571132718Skan	    cp_parser_parse_tentatively (parser);
3572132718Skan	    parser->scope = qualifying_scope;
3573132718Skan	    parser->object_scope = NULL_TREE;
3574132718Skan	    parser->qualifying_scope = NULL_TREE;
3575169689Skan	    type_decl
3576169689Skan	      = cp_parser_class_name (parser,
3577132718Skan				      /*typename_keyword_p=*/false,
3578132718Skan				      /*template_keyword_p=*/false,
3579169689Skan				      none_type,
3580132718Skan				      /*check_dependency=*/false,
3581132718Skan				      /*class_head_p=*/false,
3582132718Skan				      declarator_p);
3583132718Skan	    if (cp_parser_parse_definitely (parser))
3584146895Skan	      done = true;
3585132718Skan	  }
3586132718Skan	/* In "p->S::~T", look in the scope given by "*p" as well.  */
3587146895Skan	else if (!done && object_scope)
3588132718Skan	  {
3589132718Skan	    cp_parser_parse_tentatively (parser);
3590132718Skan	    parser->scope = object_scope;
3591132718Skan	    parser->object_scope = NULL_TREE;
3592132718Skan	    parser->qualifying_scope = NULL_TREE;
3593169689Skan	    type_decl
3594169689Skan	      = cp_parser_class_name (parser,
3595132718Skan				      /*typename_keyword_p=*/false,
3596132718Skan				      /*template_keyword_p=*/false,
3597169689Skan				      none_type,
3598132718Skan				      /*check_dependency=*/false,
3599132718Skan				      /*class_head_p=*/false,
3600132718Skan				      declarator_p);
3601132718Skan	    if (cp_parser_parse_definitely (parser))
3602146895Skan	      done = true;
3603132718Skan	  }
3604132718Skan	/* Look in the surrounding context.  */
3605146895Skan	if (!done)
3606146895Skan	  {
3607146895Skan	    parser->scope = NULL_TREE;
3608146895Skan	    parser->object_scope = NULL_TREE;
3609146895Skan	    parser->qualifying_scope = NULL_TREE;
3610169689Skan	    type_decl
3611169689Skan	      = cp_parser_class_name (parser,
3612146895Skan				      /*typename_keyword_p=*/false,
3613146895Skan				      /*template_keyword_p=*/false,
3614169689Skan				      none_type,
3615146895Skan				      /*check_dependency=*/false,
3616146895Skan				      /*class_head_p=*/false,
3617146895Skan				      declarator_p);
3618146895Skan	  }
3619132718Skan	/* If an error occurred, assume that the name of the
3620132718Skan	   destructor is the same as the name of the qualifying
3621132718Skan	   class.  That allows us to keep parsing after running
3622132718Skan	   into ill-formed destructor names.  */
3623169689Skan	if (type_decl == error_mark_node && scope)
3624132718Skan	  return build_nt (BIT_NOT_EXPR, scope);
3625132718Skan	else if (type_decl == error_mark_node)
3626132718Skan	  return error_mark_node;
3627132718Skan
3628169689Skan	/* Check that destructor name and scope match.  */
3629169689Skan	if (declarator_p && scope && !check_dtor_name (scope, type_decl))
3630169689Skan	  {
3631169689Skan	    if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3632169689Skan	      error ("declaration of %<~%T%> as member of %qT",
3633169689Skan		     type_decl, scope);
3634169689Skan	    cp_parser_simulate_error (parser);
3635169689Skan	    return error_mark_node;
3636169689Skan	  }
3637169689Skan
3638132718Skan	/* [class.dtor]
3639132718Skan
3640132718Skan	   A typedef-name that names a class shall not be used as the
3641132718Skan	   identifier in the declarator for a destructor declaration.  */
3642169689Skan	if (declarator_p
3643132718Skan	    && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3644169689Skan	    && !DECL_SELF_REFERENCE_P (type_decl)
3645169689Skan	    && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3646169689Skan	  error ("typedef-name %qD used as destructor declarator",
3647132718Skan		 type_decl);
3648132718Skan
3649132718Skan	return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3650132718Skan      }
3651132718Skan
3652132718Skan    case CPP_KEYWORD:
3653132718Skan      if (token->keyword == RID_OPERATOR)
3654132718Skan	{
3655132718Skan	  tree id;
3656132718Skan
3657132718Skan	  /* This could be a template-id, so we try that first.  */
3658132718Skan	  cp_parser_parse_tentatively (parser);
3659132718Skan	  /* Try a template-id.  */
3660132718Skan	  id = cp_parser_template_id (parser, template_keyword_p,
3661132718Skan				      /*check_dependency_p=*/true,
3662132718Skan				      declarator_p);
3663132718Skan	  /* If that worked, we're done.  */
3664132718Skan	  if (cp_parser_parse_definitely (parser))
3665132718Skan	    return id;
3666132718Skan	  /* We still don't know whether we're looking at an
3667132718Skan	     operator-function-id or a conversion-function-id.  */
3668132718Skan	  cp_parser_parse_tentatively (parser);
3669132718Skan	  /* Try an operator-function-id.  */
3670132718Skan	  id = cp_parser_operator_function_id (parser);
3671132718Skan	  /* If that didn't work, try a conversion-function-id.  */
3672132718Skan	  if (!cp_parser_parse_definitely (parser))
3673132718Skan	    id = cp_parser_conversion_function_id (parser);
3674132718Skan
3675132718Skan	  return id;
3676132718Skan	}
3677132718Skan      /* Fall through.  */
3678132718Skan
3679132718Skan    default:
3680169689Skan      if (optional_p)
3681169689Skan	return NULL_TREE;
3682132718Skan      cp_parser_error (parser, "expected unqualified-id");
3683132718Skan      return error_mark_node;
3684132718Skan    }
3685132718Skan}
3686132718Skan
3687132718Skan/* Parse an (optional) nested-name-specifier.
3688132718Skan
3689132718Skan   nested-name-specifier:
3690132718Skan     class-or-namespace-name :: nested-name-specifier [opt]
3691132718Skan     class-or-namespace-name :: template nested-name-specifier [opt]
3692132718Skan
3693132718Skan   PARSER->SCOPE should be set appropriately before this function is
3694132718Skan   called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3695132718Skan   effect.  TYPE_P is TRUE if we non-type bindings should be ignored
3696132718Skan   in name lookups.
3697132718Skan
3698132718Skan   Sets PARSER->SCOPE to the class (TYPE) or namespace
3699132718Skan   (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3700132718Skan   it unchanged if there is no nested-name-specifier.  Returns the new
3701169689Skan   scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3702132718Skan
3703132718Skan   If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3704132718Skan   part of a declaration and/or decl-specifier.  */
3705132718Skan
3706132718Skanstatic tree
3707169689Skancp_parser_nested_name_specifier_opt (cp_parser *parser,
3708169689Skan				     bool typename_keyword_p,
3709132718Skan				     bool check_dependency_p,
3710132718Skan				     bool type_p,
3711132718Skan				     bool is_declaration)
3712132718Skan{
3713132718Skan  bool success = false;
3714169689Skan  cp_token_position start = 0;
3715169689Skan  cp_token *token;
3716132718Skan
3717132718Skan  /* Remember where the nested-name-specifier starts.  */
3718169689Skan  if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3719132718Skan    {
3720169689Skan      start = cp_lexer_token_position (parser->lexer, false);
3721169689Skan      push_deferring_access_checks (dk_deferred);
3722132718Skan    }
3723132718Skan
3724132718Skan  while (true)
3725132718Skan    {
3726132718Skan      tree new_scope;
3727132718Skan      tree old_scope;
3728132718Skan      tree saved_qualifying_scope;
3729132718Skan      bool template_keyword_p;
3730132718Skan
3731132718Skan      /* Spot cases that cannot be the beginning of a
3732132718Skan	 nested-name-specifier.  */
3733132718Skan      token = cp_lexer_peek_token (parser->lexer);
3734132718Skan
3735132718Skan      /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3736132718Skan	 the already parsed nested-name-specifier.  */
3737132718Skan      if (token->type == CPP_NESTED_NAME_SPECIFIER)
3738132718Skan	{
3739132718Skan	  /* Grab the nested-name-specifier and continue the loop.  */
3740132718Skan	  cp_parser_pre_parsed_nested_name_specifier (parser);
3741169689Skan	  /* If we originally encountered this nested-name-specifier
3742169689Skan	     with IS_DECLARATION set to false, we will not have
3743169689Skan	     resolved TYPENAME_TYPEs, so we must do so here.  */
3744169689Skan	  if (is_declaration
3745169689Skan	      && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3746169689Skan	    {
3747169689Skan	      new_scope = resolve_typename_type (parser->scope,
3748169689Skan						 /*only_current_p=*/false);
3749169689Skan	      if (new_scope != error_mark_node)
3750169689Skan		parser->scope = new_scope;
3751169689Skan	    }
3752132718Skan	  success = true;
3753132718Skan	  continue;
3754132718Skan	}
3755132718Skan
3756132718Skan      /* Spot cases that cannot be the beginning of a
3757132718Skan	 nested-name-specifier.  On the second and subsequent times
3758132718Skan	 through the loop, we look for the `template' keyword.  */
3759132718Skan      if (success && token->keyword == RID_TEMPLATE)
3760132718Skan	;
3761132718Skan      /* A template-id can start a nested-name-specifier.  */
3762132718Skan      else if (token->type == CPP_TEMPLATE_ID)
3763132718Skan	;
3764132718Skan      else
3765132718Skan	{
3766132718Skan	  /* If the next token is not an identifier, then it is
3767132718Skan	     definitely not a class-or-namespace-name.  */
3768132718Skan	  if (token->type != CPP_NAME)
3769132718Skan	    break;
3770132718Skan	  /* If the following token is neither a `<' (to begin a
3771132718Skan	     template-id), nor a `::', then we are not looking at a
3772132718Skan	     nested-name-specifier.  */
3773132718Skan	  token = cp_lexer_peek_nth_token (parser->lexer, 2);
3774132718Skan	  if (token->type != CPP_SCOPE
3775132718Skan	      && !cp_parser_nth_token_starts_template_argument_list_p
3776132718Skan		  (parser, 2))
3777132718Skan	    break;
3778132718Skan	}
3779132718Skan
3780132718Skan      /* The nested-name-specifier is optional, so we parse
3781132718Skan	 tentatively.  */
3782132718Skan      cp_parser_parse_tentatively (parser);
3783132718Skan
3784132718Skan      /* Look for the optional `template' keyword, if this isn't the
3785132718Skan	 first time through the loop.  */
3786132718Skan      if (success)
3787132718Skan	template_keyword_p = cp_parser_optional_template_keyword (parser);
3788132718Skan      else
3789132718Skan	template_keyword_p = false;
3790132718Skan
3791132718Skan      /* Save the old scope since the name lookup we are about to do
3792132718Skan	 might destroy it.  */
3793132718Skan      old_scope = parser->scope;
3794132718Skan      saved_qualifying_scope = parser->qualifying_scope;
3795146895Skan      /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3796146895Skan	 look up names in "X<T>::I" in order to determine that "Y" is
3797146895Skan	 a template.  So, if we have a typename at this point, we make
3798146895Skan	 an effort to look through it.  */
3799169689Skan      if (is_declaration
3800146895Skan	  && !typename_keyword_p
3801169689Skan	  && parser->scope
3802146895Skan	  && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3803169689Skan	parser->scope = resolve_typename_type (parser->scope,
3804146895Skan					       /*only_current_p=*/false);
3805132718Skan      /* Parse the qualifying entity.  */
3806169689Skan      new_scope
3807132718Skan	= cp_parser_class_or_namespace_name (parser,
3808132718Skan					     typename_keyword_p,
3809132718Skan					     template_keyword_p,
3810132718Skan					     check_dependency_p,
3811132718Skan					     type_p,
3812132718Skan					     is_declaration);
3813132718Skan      /* Look for the `::' token.  */
3814132718Skan      cp_parser_require (parser, CPP_SCOPE, "`::'");
3815132718Skan
3816132718Skan      /* If we found what we wanted, we keep going; otherwise, we're
3817132718Skan	 done.  */
3818132718Skan      if (!cp_parser_parse_definitely (parser))
3819132718Skan	{
3820132718Skan	  bool error_p = false;
3821132718Skan
3822132718Skan	  /* Restore the OLD_SCOPE since it was valid before the
3823132718Skan	     failed attempt at finding the last
3824132718Skan	     class-or-namespace-name.  */
3825132718Skan	  parser->scope = old_scope;
3826132718Skan	  parser->qualifying_scope = saved_qualifying_scope;
3827169689Skan	  if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3828169689Skan	    break;
3829132718Skan	  /* If the next token is an identifier, and the one after
3830132718Skan	     that is a `::', then any valid interpretation would have
3831132718Skan	     found a class-or-namespace-name.  */
3832132718Skan	  while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3833169689Skan		 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3834132718Skan		     == CPP_SCOPE)
3835169689Skan		 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
3836132718Skan		     != CPP_COMPL))
3837132718Skan	    {
3838132718Skan	      token = cp_lexer_consume_token (parser->lexer);
3839169689Skan	      if (!error_p)
3840132718Skan		{
3841169689Skan		  if (!token->ambiguous_p)
3842169689Skan		    {
3843169689Skan		      tree decl;
3844169689Skan		      tree ambiguous_decls;
3845132718Skan
3846169689Skan		      decl = cp_parser_lookup_name (parser, token->u.value,
3847169689Skan						    none_type,
3848169689Skan						    /*is_template=*/false,
3849169689Skan						    /*is_namespace=*/false,
3850169689Skan						    /*check_dependency=*/true,
3851169689Skan						    &ambiguous_decls);
3852169689Skan		      if (TREE_CODE (decl) == TEMPLATE_DECL)
3853169689Skan			error ("%qD used without template parameters", decl);
3854169689Skan		      else if (ambiguous_decls)
3855169689Skan			{
3856169689Skan			  error ("reference to %qD is ambiguous",
3857169689Skan				 token->u.value);
3858169689Skan			  print_candidates (ambiguous_decls);
3859169689Skan			  decl = error_mark_node;
3860169689Skan			}
3861169689Skan		      else
3862169689Skan			cp_parser_name_lookup_error
3863169689Skan			  (parser, token->u.value, decl,
3864169689Skan			   "is not a class or namespace");
3865169689Skan		    }
3866169689Skan		  parser->scope = error_mark_node;
3867132718Skan		  error_p = true;
3868132718Skan		  /* Treat this as a successful nested-name-specifier
3869132718Skan		     due to:
3870132718Skan
3871132718Skan		     [basic.lookup.qual]
3872132718Skan
3873132718Skan		     If the name found is not a class-name (clause
3874132718Skan		     _class_) or namespace-name (_namespace.def_), the
3875132718Skan		     program is ill-formed.  */
3876132718Skan		  success = true;
3877132718Skan		}
3878132718Skan	      cp_lexer_consume_token (parser->lexer);
3879132718Skan	    }
3880132718Skan	  break;
3881132718Skan	}
3882132718Skan      /* We've found one valid nested-name-specifier.  */
3883132718Skan      success = true;
3884169689Skan      /* Name lookup always gives us a DECL.  */
3885169689Skan      if (TREE_CODE (new_scope) == TYPE_DECL)
3886169689Skan	new_scope = TREE_TYPE (new_scope);
3887169689Skan      /* Uses of "template" must be followed by actual templates.  */
3888169689Skan      if (template_keyword_p
3889169689Skan	  && !(CLASS_TYPE_P (new_scope)
3890169689Skan	       && ((CLASSTYPE_USE_TEMPLATE (new_scope)
3891169689Skan		    && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
3892169689Skan		   || CLASSTYPE_IS_TEMPLATE (new_scope)))
3893169689Skan	  && !(TREE_CODE (new_scope) == TYPENAME_TYPE
3894169689Skan	       && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
3895169689Skan		   == TEMPLATE_ID_EXPR)))
3896169689Skan	pedwarn (TYPE_P (new_scope)
3897169689Skan		 ? "%qT is not a template"
3898169689Skan		 : "%qD is not a template",
3899169689Skan		 new_scope);
3900132718Skan      /* If it is a class scope, try to complete it; we are about to
3901132718Skan	 be looking up names inside the class.  */
3902169689Skan      if (TYPE_P (new_scope)
3903132718Skan	  /* Since checking types for dependency can be expensive,
3904132718Skan	     avoid doing it if the type is already complete.  */
3905169689Skan	  && !COMPLETE_TYPE_P (new_scope)
3906132718Skan	  /* Do not try to complete dependent types.  */
3907169689Skan	  && !dependent_type_p (new_scope))
3908169689Skan	new_scope = complete_type (new_scope);
3909169689Skan      /* Make sure we look in the right scope the next time through
3910169689Skan	 the loop.  */
3911169689Skan      parser->scope = new_scope;
3912132718Skan    }
3913132718Skan
3914132718Skan  /* If parsing tentatively, replace the sequence of tokens that makes
3915132718Skan     up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3916132718Skan     token.  That way, should we re-parse the token stream, we will
3917132718Skan     not have to repeat the effort required to do the parse, nor will
3918132718Skan     we issue duplicate error messages.  */
3919169689Skan  if (success && start)
3920132718Skan    {
3921169689Skan      cp_token *token;
3922132718Skan
3923169689Skan      token = cp_lexer_token_at (parser->lexer, start);
3924132718Skan      /* Reset the contents of the START token.  */
3925132718Skan      token->type = CPP_NESTED_NAME_SPECIFIER;
3926169689Skan      /* Retrieve any deferred checks.  Do not pop this access checks yet
3927169689Skan	 so the memory will not be reclaimed during token replacing below.  */
3928169689Skan      token->u.tree_check_value = GGC_CNEW (struct tree_check);
3929169689Skan      token->u.tree_check_value->value = parser->scope;
3930169689Skan      token->u.tree_check_value->checks = get_deferred_access_checks ();
3931169689Skan      token->u.tree_check_value->qualifying_scope =
3932169689Skan	parser->qualifying_scope;
3933132718Skan      token->keyword = RID_MAX;
3934169689Skan
3935132718Skan      /* Purge all subsequent tokens.  */
3936169689Skan      cp_lexer_purge_tokens_after (parser->lexer, start);
3937132718Skan    }
3938132718Skan
3939169689Skan  if (start)
3940169689Skan    pop_to_parent_deferring_access_checks ();
3941169689Skan
3942132718Skan  return success ? parser->scope : NULL_TREE;
3943132718Skan}
3944132718Skan
3945132718Skan/* Parse a nested-name-specifier.  See
3946132718Skan   cp_parser_nested_name_specifier_opt for details.  This function
3947132718Skan   behaves identically, except that it will an issue an error if no
3948169689Skan   nested-name-specifier is present.  */
3949132718Skan
3950132718Skanstatic tree
3951169689Skancp_parser_nested_name_specifier (cp_parser *parser,
3952169689Skan				 bool typename_keyword_p,
3953132718Skan				 bool check_dependency_p,
3954132718Skan				 bool type_p,
3955132718Skan				 bool is_declaration)
3956132718Skan{
3957132718Skan  tree scope;
3958132718Skan
3959132718Skan  /* Look for the nested-name-specifier.  */
3960132718Skan  scope = cp_parser_nested_name_specifier_opt (parser,
3961132718Skan					       typename_keyword_p,
3962132718Skan					       check_dependency_p,
3963132718Skan					       type_p,
3964132718Skan					       is_declaration);
3965132718Skan  /* If it was not present, issue an error message.  */
3966132718Skan  if (!scope)
3967132718Skan    {
3968132718Skan      cp_parser_error (parser, "expected nested-name-specifier");
3969132718Skan      parser->scope = NULL_TREE;
3970132718Skan    }
3971132718Skan
3972132718Skan  return scope;
3973132718Skan}
3974132718Skan
3975132718Skan/* Parse a class-or-namespace-name.
3976132718Skan
3977132718Skan   class-or-namespace-name:
3978132718Skan     class-name
3979132718Skan     namespace-name
3980132718Skan
3981132718Skan   TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3982132718Skan   TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3983132718Skan   CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3984132718Skan   TYPE_P is TRUE iff the next name should be taken as a class-name,
3985132718Skan   even the same name is declared to be another entity in the same
3986132718Skan   scope.
3987132718Skan
3988132718Skan   Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3989132718Skan   specified by the class-or-namespace-name.  If neither is found the
3990132718Skan   ERROR_MARK_NODE is returned.  */
3991132718Skan
3992132718Skanstatic tree
3993169689Skancp_parser_class_or_namespace_name (cp_parser *parser,
3994132718Skan				   bool typename_keyword_p,
3995132718Skan				   bool template_keyword_p,
3996132718Skan				   bool check_dependency_p,
3997132718Skan				   bool type_p,
3998132718Skan				   bool is_declaration)
3999132718Skan{
4000132718Skan  tree saved_scope;
4001132718Skan  tree saved_qualifying_scope;
4002132718Skan  tree saved_object_scope;
4003132718Skan  tree scope;
4004132718Skan  bool only_class_p;
4005132718Skan
4006132718Skan  /* Before we try to parse the class-name, we must save away the
4007132718Skan     current PARSER->SCOPE since cp_parser_class_name will destroy
4008132718Skan     it.  */
4009132718Skan  saved_scope = parser->scope;
4010132718Skan  saved_qualifying_scope = parser->qualifying_scope;
4011132718Skan  saved_object_scope = parser->object_scope;
4012132718Skan  /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
4013132718Skan     there is no need to look for a namespace-name.  */
4014132718Skan  only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
4015132718Skan  if (!only_class_p)
4016132718Skan    cp_parser_parse_tentatively (parser);
4017169689Skan  scope = cp_parser_class_name (parser,
4018132718Skan				typename_keyword_p,
4019132718Skan				template_keyword_p,
4020169689Skan				type_p ? class_type : none_type,
4021132718Skan				check_dependency_p,
4022132718Skan				/*class_head_p=*/false,
4023132718Skan				is_declaration);
4024132718Skan  /* If that didn't work, try for a namespace-name.  */
4025132718Skan  if (!only_class_p && !cp_parser_parse_definitely (parser))
4026132718Skan    {
4027132718Skan      /* Restore the saved scope.  */
4028132718Skan      parser->scope = saved_scope;
4029132718Skan      parser->qualifying_scope = saved_qualifying_scope;
4030132718Skan      parser->object_scope = saved_object_scope;
4031132718Skan      /* If we are not looking at an identifier followed by the scope
4032132718Skan	 resolution operator, then this is not part of a
4033132718Skan	 nested-name-specifier.  (Note that this function is only used
4034132718Skan	 to parse the components of a nested-name-specifier.)  */
4035132718Skan      if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4036132718Skan	  || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4037132718Skan	return error_mark_node;
4038132718Skan      scope = cp_parser_namespace_name (parser);
4039132718Skan    }
4040132718Skan
4041132718Skan  return scope;
4042132718Skan}
4043132718Skan
4044132718Skan/* Parse a postfix-expression.
4045132718Skan
4046132718Skan   postfix-expression:
4047132718Skan     primary-expression
4048132718Skan     postfix-expression [ expression ]
4049132718Skan     postfix-expression ( expression-list [opt] )
4050132718Skan     simple-type-specifier ( expression-list [opt] )
4051169689Skan     typename :: [opt] nested-name-specifier identifier
4052132718Skan       ( expression-list [opt] )
4053132718Skan     typename :: [opt] nested-name-specifier template [opt] template-id
4054132718Skan       ( expression-list [opt] )
4055132718Skan     postfix-expression . template [opt] id-expression
4056132718Skan     postfix-expression -> template [opt] id-expression
4057132718Skan     postfix-expression . pseudo-destructor-name
4058132718Skan     postfix-expression -> pseudo-destructor-name
4059132718Skan     postfix-expression ++
4060132718Skan     postfix-expression --
4061132718Skan     dynamic_cast < type-id > ( expression )
4062132718Skan     static_cast < type-id > ( expression )
4063132718Skan     reinterpret_cast < type-id > ( expression )
4064132718Skan     const_cast < type-id > ( expression )
4065132718Skan     typeid ( expression )
4066132718Skan     typeid ( type-id )
4067132718Skan
4068132718Skan   GNU Extension:
4069169689Skan
4070132718Skan   postfix-expression:
4071132718Skan     ( type-id ) { initializer-list , [opt] }
4072132718Skan
4073132718Skan   This extension is a GNU version of the C99 compound-literal
4074132718Skan   construct.  (The C99 grammar uses `type-name' instead of `type-id',
4075132718Skan   but they are essentially the same concept.)
4076132718Skan
4077132718Skan   If ADDRESS_P is true, the postfix expression is the operand of the
4078169689Skan   `&' operator.  CAST_P is true if this expression is the target of a
4079169689Skan   cast.
4080132718Skan
4081132718Skan   Returns a representation of the expression.  */
4082132718Skan
4083132718Skanstatic tree
4084169689Skancp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p)
4085132718Skan{
4086132718Skan  cp_token *token;
4087132718Skan  enum rid keyword;
4088132718Skan  cp_id_kind idk = CP_ID_KIND_NONE;
4089132718Skan  tree postfix_expression = NULL_TREE;
4090132718Skan
4091132718Skan  /* Peek at the next token.  */
4092132718Skan  token = cp_lexer_peek_token (parser->lexer);
4093132718Skan  /* Some of the productions are determined by keywords.  */
4094132718Skan  keyword = token->keyword;
4095132718Skan  switch (keyword)
4096132718Skan    {
4097132718Skan    case RID_DYNCAST:
4098132718Skan    case RID_STATCAST:
4099132718Skan    case RID_REINTCAST:
4100132718Skan    case RID_CONSTCAST:
4101132718Skan      {
4102132718Skan	tree type;
4103132718Skan	tree expression;
4104132718Skan	const char *saved_message;
4105132718Skan
4106132718Skan	/* All of these can be handled in the same way from the point
4107132718Skan	   of view of parsing.  Begin by consuming the token
4108132718Skan	   identifying the cast.  */
4109132718Skan	cp_lexer_consume_token (parser->lexer);
4110169689Skan
4111132718Skan	/* New types cannot be defined in the cast.  */
4112132718Skan	saved_message = parser->type_definition_forbidden_message;
4113132718Skan	parser->type_definition_forbidden_message
4114132718Skan	  = "types may not be defined in casts";
4115132718Skan
4116132718Skan	/* Look for the opening `<'.  */
4117132718Skan	cp_parser_require (parser, CPP_LESS, "`<'");
4118132718Skan	/* Parse the type to which we are casting.  */
4119132718Skan	type = cp_parser_type_id (parser);
4120132718Skan	/* Look for the closing `>'.  */
4121132718Skan	cp_parser_require (parser, CPP_GREATER, "`>'");
4122132718Skan	/* Restore the old message.  */
4123132718Skan	parser->type_definition_forbidden_message = saved_message;
4124132718Skan
4125132718Skan	/* And the expression which is being cast.  */
4126132718Skan	cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4127169689Skan	expression = cp_parser_expression (parser, /*cast_p=*/true);
4128132718Skan	cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4129132718Skan
4130132718Skan	/* Only type conversions to integral or enumeration types
4131132718Skan	   can be used in constant-expressions.  */
4132169689Skan	if (!cast_valid_in_integral_constant_expression_p (type)
4133169689Skan	    && (cp_parser_non_integral_constant_expression
4134132718Skan		(parser,
4135132718Skan		 "a cast to a type other than an integral or "
4136132718Skan		 "enumeration type")))
4137132718Skan	  return error_mark_node;
4138132718Skan
4139132718Skan	switch (keyword)
4140132718Skan	  {
4141132718Skan	  case RID_DYNCAST:
4142132718Skan	    postfix_expression
4143132718Skan	      = build_dynamic_cast (type, expression);
4144132718Skan	    break;
4145132718Skan	  case RID_STATCAST:
4146132718Skan	    postfix_expression
4147132718Skan	      = build_static_cast (type, expression);
4148132718Skan	    break;
4149132718Skan	  case RID_REINTCAST:
4150132718Skan	    postfix_expression
4151132718Skan	      = build_reinterpret_cast (type, expression);
4152132718Skan	    break;
4153132718Skan	  case RID_CONSTCAST:
4154132718Skan	    postfix_expression
4155132718Skan	      = build_const_cast (type, expression);
4156132718Skan	    break;
4157132718Skan	  default:
4158169689Skan	    gcc_unreachable ();
4159132718Skan	  }
4160132718Skan      }
4161132718Skan      break;
4162132718Skan
4163132718Skan    case RID_TYPEID:
4164132718Skan      {
4165132718Skan	tree type;
4166132718Skan	const char *saved_message;
4167132718Skan	bool saved_in_type_id_in_expr_p;
4168132718Skan
4169132718Skan	/* Consume the `typeid' token.  */
4170132718Skan	cp_lexer_consume_token (parser->lexer);
4171132718Skan	/* Look for the `(' token.  */
4172132718Skan	cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4173132718Skan	/* Types cannot be defined in a `typeid' expression.  */
4174132718Skan	saved_message = parser->type_definition_forbidden_message;
4175132718Skan	parser->type_definition_forbidden_message
4176132718Skan	  = "types may not be defined in a `typeid\' expression";
4177132718Skan	/* We can't be sure yet whether we're looking at a type-id or an
4178132718Skan	   expression.  */
4179132718Skan	cp_parser_parse_tentatively (parser);
4180132718Skan	/* Try a type-id first.  */
4181132718Skan	saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4182132718Skan	parser->in_type_id_in_expr_p = true;
4183132718Skan	type = cp_parser_type_id (parser);
4184132718Skan	parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4185132718Skan	/* Look for the `)' token.  Otherwise, we can't be sure that
4186132718Skan	   we're not looking at an expression: consider `typeid (int
4187132718Skan	   (3))', for example.  */
4188132718Skan	cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4189132718Skan	/* If all went well, simply lookup the type-id.  */
4190132718Skan	if (cp_parser_parse_definitely (parser))
4191132718Skan	  postfix_expression = get_typeid (type);
4192132718Skan	/* Otherwise, fall back to the expression variant.  */
4193132718Skan	else
4194132718Skan	  {
4195132718Skan	    tree expression;
4196132718Skan
4197132718Skan	    /* Look for an expression.  */
4198169689Skan	    expression = cp_parser_expression (parser, /*cast_p=*/false);
4199132718Skan	    /* Compute its typeid.  */
4200132718Skan	    postfix_expression = build_typeid (expression);
4201132718Skan	    /* Look for the `)' token.  */
4202132718Skan	    cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4203132718Skan	  }
4204169689Skan	/* Restore the saved message.  */
4205169689Skan	parser->type_definition_forbidden_message = saved_message;
4206132718Skan	/* `typeid' may not appear in an integral constant expression.  */
4207169689Skan	if (cp_parser_non_integral_constant_expression(parser,
4208132718Skan						       "`typeid' operator"))
4209132718Skan	  return error_mark_node;
4210132718Skan      }
4211132718Skan      break;
4212169689Skan
4213132718Skan    case RID_TYPENAME:
4214132718Skan      {
4215132718Skan	tree type;
4216161651Skan	/* The syntax permitted here is the same permitted for an
4217161651Skan	   elaborated-type-specifier.  */
4218161651Skan	type = cp_parser_elaborated_type_specifier (parser,
4219161651Skan						    /*is_friend=*/false,
4220161651Skan						    /*is_declaration=*/false);
4221132718Skan	postfix_expression = cp_parser_functional_cast (parser, type);
4222132718Skan      }
4223132718Skan      break;
4224132718Skan
4225132718Skan    default:
4226132718Skan      {
4227132718Skan	tree type;
4228132718Skan
4229132718Skan	/* If the next thing is a simple-type-specifier, we may be
4230132718Skan	   looking at a functional cast.  We could also be looking at
4231132718Skan	   an id-expression.  So, we try the functional cast, and if
4232132718Skan	   that doesn't work we fall back to the primary-expression.  */
4233132718Skan	cp_parser_parse_tentatively (parser);
4234132718Skan	/* Look for the simple-type-specifier.  */
4235169689Skan	type = cp_parser_simple_type_specifier (parser,
4236169689Skan						/*decl_specs=*/NULL,
4237169689Skan						CP_PARSER_FLAGS_NONE);
4238132718Skan	/* Parse the cast itself.  */
4239132718Skan	if (!cp_parser_error_occurred (parser))
4240169689Skan	  postfix_expression
4241132718Skan	    = cp_parser_functional_cast (parser, type);
4242132718Skan	/* If that worked, we're done.  */
4243132718Skan	if (cp_parser_parse_definitely (parser))
4244132718Skan	  break;
4245132718Skan
4246132718Skan	/* If the functional-cast didn't work out, try a
4247132718Skan	   compound-literal.  */
4248132718Skan	if (cp_parser_allow_gnu_extensions_p (parser)
4249132718Skan	    && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4250132718Skan	  {
4251169689Skan	    VEC(constructor_elt,gc) *initializer_list = NULL;
4252132718Skan	    bool saved_in_type_id_in_expr_p;
4253132718Skan
4254132718Skan	    cp_parser_parse_tentatively (parser);
4255132718Skan	    /* Consume the `('.  */
4256132718Skan	    cp_lexer_consume_token (parser->lexer);
4257132718Skan	    /* Parse the type.  */
4258132718Skan	    saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4259132718Skan	    parser->in_type_id_in_expr_p = true;
4260132718Skan	    type = cp_parser_type_id (parser);
4261132718Skan	    parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4262132718Skan	    /* Look for the `)'.  */
4263132718Skan	    cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4264132718Skan	    /* Look for the `{'.  */
4265132718Skan	    cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
4266132718Skan	    /* If things aren't going well, there's no need to
4267132718Skan	       keep going.  */
4268132718Skan	    if (!cp_parser_error_occurred (parser))
4269132718Skan	      {
4270132718Skan		bool non_constant_p;
4271132718Skan		/* Parse the initializer-list.  */
4272169689Skan		initializer_list
4273132718Skan		  = cp_parser_initializer_list (parser, &non_constant_p);
4274132718Skan		/* Allow a trailing `,'.  */
4275132718Skan		if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4276132718Skan		  cp_lexer_consume_token (parser->lexer);
4277132718Skan		/* Look for the final `}'.  */
4278132718Skan		cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
4279132718Skan	      }
4280132718Skan	    /* If that worked, we're definitely looking at a
4281132718Skan	       compound-literal expression.  */
4282132718Skan	    if (cp_parser_parse_definitely (parser))
4283132718Skan	      {
4284132718Skan		/* Warn the user that a compound literal is not
4285132718Skan		   allowed in standard C++.  */
4286132718Skan		if (pedantic)
4287132718Skan		  pedwarn ("ISO C++ forbids compound-literals");
4288169689Skan		/* For simplicitly, we disallow compound literals in
4289169689Skan		   constant-expressions for simpliicitly.  We could
4290169689Skan		   allow compound literals of integer type, whose
4291169689Skan		   initializer was a constant, in constant
4292169689Skan		   expressions.  Permitting that usage, as a further
4293169689Skan		   extension, would not change the meaning of any
4294169689Skan		   currently accepted programs.  (Of course, as
4295169689Skan		   compound literals are not part of ISO C++, the
4296169689Skan		   standard has nothing to say.)  */
4297169689Skan		if (cp_parser_non_integral_constant_expression
4298169689Skan		    (parser, "non-constant compound literals"))
4299169689Skan		  {
4300169689Skan		    postfix_expression = error_mark_node;
4301169689Skan		    break;
4302169689Skan		  }
4303132718Skan		/* Form the representation of the compound-literal.  */
4304169689Skan		postfix_expression
4305132718Skan		  = finish_compound_literal (type, initializer_list);
4306132718Skan		break;
4307132718Skan	      }
4308132718Skan	  }
4309132718Skan
4310132718Skan	/* It must be a primary-expression.  */
4311169689Skan	postfix_expression
4312169689Skan	  = cp_parser_primary_expression (parser, address_p, cast_p,
4313169689Skan					  /*template_arg_p=*/false,
4314169689Skan					  &idk);
4315132718Skan      }
4316132718Skan      break;
4317132718Skan    }
4318132718Skan
4319132718Skan  /* Keep looping until the postfix-expression is complete.  */
4320132718Skan  while (true)
4321132718Skan    {
4322132718Skan      if (idk == CP_ID_KIND_UNQUALIFIED
4323132718Skan	  && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4324132718Skan	  && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4325132718Skan	/* It is not a Koenig lookup function call.  */
4326169689Skan	postfix_expression
4327132718Skan	  = unqualified_name_lookup_error (postfix_expression);
4328169689Skan
4329132718Skan      /* Peek at the next token.  */
4330132718Skan      token = cp_lexer_peek_token (parser->lexer);
4331132718Skan
4332132718Skan      switch (token->type)
4333132718Skan	{
4334132718Skan	case CPP_OPEN_SQUARE:
4335169689Skan	  postfix_expression
4336169689Skan	    = cp_parser_postfix_open_square_expression (parser,
4337169689Skan							postfix_expression,
4338169689Skan							false);
4339169689Skan	  idk = CP_ID_KIND_NONE;
4340132718Skan	  break;
4341132718Skan
4342132718Skan	case CPP_OPEN_PAREN:
4343132718Skan	  /* postfix-expression ( expression-list [opt] ) */
4344132718Skan	  {
4345132718Skan	    bool koenig_p;
4346169689Skan	    bool is_builtin_constant_p;
4347169689Skan	    bool saved_integral_constant_expression_p = false;
4348169689Skan	    bool saved_non_integral_constant_expression_p = false;
4349169689Skan	    tree args;
4350132718Skan
4351169689Skan	    is_builtin_constant_p
4352169689Skan	      = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4353169689Skan	    if (is_builtin_constant_p)
4354169689Skan	      {
4355169689Skan		/* The whole point of __builtin_constant_p is to allow
4356169689Skan		   non-constant expressions to appear as arguments.  */
4357169689Skan		saved_integral_constant_expression_p
4358169689Skan		  = parser->integral_constant_expression_p;
4359169689Skan		saved_non_integral_constant_expression_p
4360169689Skan		  = parser->non_integral_constant_expression_p;
4361169689Skan		parser->integral_constant_expression_p = false;
4362169689Skan	      }
4363169689Skan	    args = (cp_parser_parenthesized_expression_list
4364169689Skan		    (parser, /*is_attribute_list=*/false,
4365169689Skan		     /*cast_p=*/false,
4366169689Skan		     /*non_constant_p=*/NULL));
4367169689Skan	    if (is_builtin_constant_p)
4368169689Skan	      {
4369169689Skan		parser->integral_constant_expression_p
4370169689Skan		  = saved_integral_constant_expression_p;
4371169689Skan		parser->non_integral_constant_expression_p
4372169689Skan		  = saved_non_integral_constant_expression_p;
4373169689Skan	      }
4374169689Skan
4375132718Skan	    if (args == error_mark_node)
4376132718Skan	      {
4377132718Skan		postfix_expression = error_mark_node;
4378132718Skan		break;
4379132718Skan	      }
4380169689Skan
4381132718Skan	    /* Function calls are not permitted in
4382132718Skan	       constant-expressions.  */
4383169689Skan	    if (! builtin_valid_in_constant_expr_p (postfix_expression)
4384169689Skan		&& cp_parser_non_integral_constant_expression (parser,
4385169689Skan							       "a function call"))
4386132718Skan	      {
4387132718Skan		postfix_expression = error_mark_node;
4388132718Skan		break;
4389132718Skan	      }
4390132718Skan
4391132718Skan	    koenig_p = false;
4392132718Skan	    if (idk == CP_ID_KIND_UNQUALIFIED)
4393132718Skan	      {
4394146895Skan		if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4395146895Skan		  {
4396146895Skan		    if (args)
4397146895Skan		      {
4398146895Skan			koenig_p = true;
4399146895Skan			postfix_expression
4400146895Skan			  = perform_koenig_lookup (postfix_expression, args);
4401146895Skan		      }
4402146895Skan		    else
4403146895Skan		      postfix_expression
4404146895Skan			= unqualified_fn_lookup_error (postfix_expression);
4405169689Skan		  }
4406132718Skan		/* We do not perform argument-dependent lookup if
4407132718Skan		   normal lookup finds a non-function, in accordance
4408132718Skan		   with the expected resolution of DR 218.  */
4409146895Skan		else if (args && is_overloaded_fn (postfix_expression))
4410132718Skan		  {
4411146895Skan		    tree fn = get_first_fn (postfix_expression);
4412169689Skan
4413146895Skan		    if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4414146895Skan		      fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4415169689Skan
4416146895Skan		    /* Only do argument dependent lookup if regular
4417146895Skan		       lookup does not find a set of member functions.
4418146895Skan		       [basic.lookup.koenig]/2a  */
4419169689Skan		    if (!DECL_FUNCTION_MEMBER_P (fn))
4420169689Skan		      {
4421169689Skan			koenig_p = true;
4422169689Skan			postfix_expression
4423169689Skan			  = perform_koenig_lookup (postfix_expression, args);
4424169689Skan		      }
4425132718Skan		  }
4426132718Skan	      }
4427169689Skan
4428132718Skan	    if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4429132718Skan	      {
4430132718Skan		tree instance = TREE_OPERAND (postfix_expression, 0);
4431132718Skan		tree fn = TREE_OPERAND (postfix_expression, 1);
4432132718Skan
4433132718Skan		if (processing_template_decl
4434132718Skan		    && (type_dependent_expression_p (instance)
4435132718Skan			|| (!BASELINK_P (fn)
4436132718Skan			    && TREE_CODE (fn) != FIELD_DECL)
4437132718Skan			|| type_dependent_expression_p (fn)
4438132718Skan			|| any_type_dependent_arguments_p (args)))
4439132718Skan		  {
4440132718Skan		    postfix_expression
4441169689Skan		      = build_min_nt (CALL_EXPR, postfix_expression,
4442169689Skan				      args, NULL_TREE);
4443132718Skan		    break;
4444132718Skan		  }
4445132718Skan
4446132718Skan		if (BASELINK_P (fn))
4447132718Skan		  postfix_expression
4448169689Skan		    = (build_new_method_call
4449169689Skan		       (instance, fn, args, NULL_TREE,
4450169689Skan			(idk == CP_ID_KIND_QUALIFIED
4451169689Skan			 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4452169689Skan			/*fn_p=*/NULL));
4453132718Skan		else
4454132718Skan		  postfix_expression
4455132718Skan		    = finish_call_expr (postfix_expression, args,
4456132718Skan					/*disallow_virtual=*/false,
4457132718Skan					/*koenig_p=*/false);
4458132718Skan	      }
4459132718Skan	    else if (TREE_CODE (postfix_expression) == OFFSET_REF
4460132718Skan		     || TREE_CODE (postfix_expression) == MEMBER_REF
4461132718Skan		     || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4462132718Skan	      postfix_expression = (build_offset_ref_call_from_tree
4463132718Skan				    (postfix_expression, args));
4464132718Skan	    else if (idk == CP_ID_KIND_QUALIFIED)
4465132718Skan	      /* A call to a static class member, or a namespace-scope
4466132718Skan		 function.  */
4467132718Skan	      postfix_expression
4468132718Skan		= finish_call_expr (postfix_expression, args,
4469132718Skan				    /*disallow_virtual=*/true,
4470132718Skan				    koenig_p);
4471132718Skan	    else
4472132718Skan	      /* All other function calls.  */
4473169689Skan	      postfix_expression
4474169689Skan		= finish_call_expr (postfix_expression, args,
4475132718Skan				    /*disallow_virtual=*/false,
4476132718Skan				    koenig_p);
4477132718Skan
4478132718Skan	    /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
4479132718Skan	    idk = CP_ID_KIND_NONE;
4480132718Skan	  }
4481132718Skan	  break;
4482169689Skan
4483132718Skan	case CPP_DOT:
4484132718Skan	case CPP_DEREF:
4485169689Skan	  /* postfix-expression . template [opt] id-expression
4486169689Skan	     postfix-expression . pseudo-destructor-name
4487132718Skan	     postfix-expression -> template [opt] id-expression
4488132718Skan	     postfix-expression -> pseudo-destructor-name */
4489132718Skan
4490169689Skan	  /* Consume the `.' or `->' operator.  */
4491169689Skan	  cp_lexer_consume_token (parser->lexer);
4492132718Skan
4493169689Skan	  postfix_expression
4494169689Skan	    = cp_parser_postfix_dot_deref_expression (parser, token->type,
4495169689Skan						      postfix_expression,
4496169689Skan						      false, &idk);
4497132718Skan	  break;
4498132718Skan
4499132718Skan	case CPP_PLUS_PLUS:
4500132718Skan	  /* postfix-expression ++  */
4501132718Skan	  /* Consume the `++' token.  */
4502132718Skan	  cp_lexer_consume_token (parser->lexer);
4503132718Skan	  /* Generate a representation for the complete expression.  */
4504169689Skan	  postfix_expression
4505169689Skan	    = finish_increment_expr (postfix_expression,
4506132718Skan				     POSTINCREMENT_EXPR);
4507132718Skan	  /* Increments may not appear in constant-expressions.  */
4508132718Skan	  if (cp_parser_non_integral_constant_expression (parser,
4509132718Skan							  "an increment"))
4510132718Skan	    postfix_expression = error_mark_node;
4511132718Skan	  idk = CP_ID_KIND_NONE;
4512132718Skan	  break;
4513132718Skan
4514132718Skan	case CPP_MINUS_MINUS:
4515132718Skan	  /* postfix-expression -- */
4516132718Skan	  /* Consume the `--' token.  */
4517132718Skan	  cp_lexer_consume_token (parser->lexer);
4518132718Skan	  /* Generate a representation for the complete expression.  */
4519169689Skan	  postfix_expression
4520169689Skan	    = finish_increment_expr (postfix_expression,
4521132718Skan				     POSTDECREMENT_EXPR);
4522132718Skan	  /* Decrements may not appear in constant-expressions.  */
4523132718Skan	  if (cp_parser_non_integral_constant_expression (parser,
4524132718Skan							  "a decrement"))
4525132718Skan	    postfix_expression = error_mark_node;
4526132718Skan	  idk = CP_ID_KIND_NONE;
4527132718Skan	  break;
4528132718Skan
4529132718Skan	default:
4530132718Skan	  return postfix_expression;
4531132718Skan	}
4532132718Skan    }
4533132718Skan
4534132718Skan  /* We should never get here.  */
4535169689Skan  gcc_unreachable ();
4536132718Skan  return error_mark_node;
4537132718Skan}
4538132718Skan
4539169689Skan/* A subroutine of cp_parser_postfix_expression that also gets hijacked
4540169689Skan   by cp_parser_builtin_offsetof.  We're looking for
4541169689Skan
4542169689Skan     postfix-expression [ expression ]
4543169689Skan
4544169689Skan   FOR_OFFSETOF is set if we're being called in that context, which
4545169689Skan   changes how we deal with integer constant expressions.  */
4546169689Skan
4547169689Skanstatic tree
4548169689Skancp_parser_postfix_open_square_expression (cp_parser *parser,
4549169689Skan					  tree postfix_expression,
4550169689Skan					  bool for_offsetof)
4551169689Skan{
4552169689Skan  tree index;
4553169689Skan
4554169689Skan  /* Consume the `[' token.  */
4555169689Skan  cp_lexer_consume_token (parser->lexer);
4556169689Skan
4557169689Skan  /* Parse the index expression.  */
4558169689Skan  /* ??? For offsetof, there is a question of what to allow here.  If
4559169689Skan     offsetof is not being used in an integral constant expression context,
4560169689Skan     then we *could* get the right answer by computing the value at runtime.
4561169689Skan     If we are in an integral constant expression context, then we might
4562169689Skan     could accept any constant expression; hard to say without analysis.
4563169689Skan     Rather than open the barn door too wide right away, allow only integer
4564169689Skan     constant expressions here.  */
4565169689Skan  if (for_offsetof)
4566169689Skan    index = cp_parser_constant_expression (parser, false, NULL);
4567169689Skan  else
4568169689Skan    index = cp_parser_expression (parser, /*cast_p=*/false);
4569169689Skan
4570169689Skan  /* Look for the closing `]'.  */
4571169689Skan  cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4572169689Skan
4573169689Skan  /* Build the ARRAY_REF.  */
4574169689Skan  postfix_expression = grok_array_decl (postfix_expression, index);
4575169689Skan
4576169689Skan  /* When not doing offsetof, array references are not permitted in
4577169689Skan     constant-expressions.  */
4578169689Skan  if (!for_offsetof
4579169689Skan      && (cp_parser_non_integral_constant_expression
4580169689Skan	  (parser, "an array reference")))
4581169689Skan    postfix_expression = error_mark_node;
4582169689Skan
4583169689Skan  return postfix_expression;
4584169689Skan}
4585169689Skan
4586169689Skan/* A subroutine of cp_parser_postfix_expression that also gets hijacked
4587169689Skan   by cp_parser_builtin_offsetof.  We're looking for
4588169689Skan
4589169689Skan     postfix-expression . template [opt] id-expression
4590169689Skan     postfix-expression . pseudo-destructor-name
4591169689Skan     postfix-expression -> template [opt] id-expression
4592169689Skan     postfix-expression -> pseudo-destructor-name
4593169689Skan
4594169689Skan   FOR_OFFSETOF is set if we're being called in that context.  That sorta
4595169689Skan   limits what of the above we'll actually accept, but nevermind.
4596169689Skan   TOKEN_TYPE is the "." or "->" token, which will already have been
4597169689Skan   removed from the stream.  */
4598169689Skan
4599169689Skanstatic tree
4600169689Skancp_parser_postfix_dot_deref_expression (cp_parser *parser,
4601169689Skan					enum cpp_ttype token_type,
4602169689Skan					tree postfix_expression,
4603169689Skan					bool for_offsetof, cp_id_kind *idk)
4604169689Skan{
4605169689Skan  tree name;
4606169689Skan  bool dependent_p;
4607169689Skan  bool pseudo_destructor_p;
4608169689Skan  tree scope = NULL_TREE;
4609169689Skan
4610169689Skan  /* If this is a `->' operator, dereference the pointer.  */
4611169689Skan  if (token_type == CPP_DEREF)
4612169689Skan    postfix_expression = build_x_arrow (postfix_expression);
4613169689Skan  /* Check to see whether or not the expression is type-dependent.  */
4614169689Skan  dependent_p = type_dependent_expression_p (postfix_expression);
4615169689Skan  /* The identifier following the `->' or `.' is not qualified.  */
4616169689Skan  parser->scope = NULL_TREE;
4617169689Skan  parser->qualifying_scope = NULL_TREE;
4618169689Skan  parser->object_scope = NULL_TREE;
4619169689Skan  *idk = CP_ID_KIND_NONE;
4620169689Skan  /* Enter the scope corresponding to the type of the object
4621169689Skan     given by the POSTFIX_EXPRESSION.  */
4622169689Skan  if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4623169689Skan    {
4624169689Skan      scope = TREE_TYPE (postfix_expression);
4625169689Skan      /* According to the standard, no expression should ever have
4626169689Skan	 reference type.  Unfortunately, we do not currently match
4627169689Skan	 the standard in this respect in that our internal representation
4628169689Skan	 of an expression may have reference type even when the standard
4629169689Skan	 says it does not.  Therefore, we have to manually obtain the
4630169689Skan	 underlying type here.  */
4631169689Skan      scope = non_reference (scope);
4632169689Skan      /* The type of the POSTFIX_EXPRESSION must be complete.  */
4633169689Skan      if (scope == unknown_type_node)
4634169689Skan	{
4635169689Skan	  error ("%qE does not have class type", postfix_expression);
4636169689Skan	  scope = NULL_TREE;
4637169689Skan	}
4638169689Skan      else
4639169689Skan	scope = complete_type_or_else (scope, NULL_TREE);
4640169689Skan      /* Let the name lookup machinery know that we are processing a
4641169689Skan	 class member access expression.  */
4642169689Skan      parser->context->object_type = scope;
4643169689Skan      /* If something went wrong, we want to be able to discern that case,
4644169689Skan	 as opposed to the case where there was no SCOPE due to the type
4645169689Skan	 of expression being dependent.  */
4646169689Skan      if (!scope)
4647169689Skan	scope = error_mark_node;
4648169689Skan      /* If the SCOPE was erroneous, make the various semantic analysis
4649169689Skan	 functions exit quickly -- and without issuing additional error
4650169689Skan	 messages.  */
4651169689Skan      if (scope == error_mark_node)
4652169689Skan	postfix_expression = error_mark_node;
4653169689Skan    }
4654169689Skan
4655169689Skan  /* Assume this expression is not a pseudo-destructor access.  */
4656169689Skan  pseudo_destructor_p = false;
4657169689Skan
4658169689Skan  /* If the SCOPE is a scalar type, then, if this is a valid program,
4659169689Skan     we must be looking at a pseudo-destructor-name.  */
4660169689Skan  if (scope && SCALAR_TYPE_P (scope))
4661169689Skan    {
4662169689Skan      tree s;
4663169689Skan      tree type;
4664169689Skan
4665169689Skan      cp_parser_parse_tentatively (parser);
4666169689Skan      /* Parse the pseudo-destructor-name.  */
4667169689Skan      s = NULL_TREE;
4668169689Skan      cp_parser_pseudo_destructor_name (parser, &s, &type);
4669169689Skan      if (cp_parser_parse_definitely (parser))
4670169689Skan	{
4671169689Skan	  pseudo_destructor_p = true;
4672169689Skan	  postfix_expression
4673169689Skan	    = finish_pseudo_destructor_expr (postfix_expression,
4674169689Skan					     s, TREE_TYPE (type));
4675169689Skan	}
4676169689Skan    }
4677169689Skan
4678169689Skan  if (!pseudo_destructor_p)
4679169689Skan    {
4680169689Skan      /* If the SCOPE is not a scalar type, we are looking at an
4681169689Skan	 ordinary class member access expression, rather than a
4682169689Skan	 pseudo-destructor-name.  */
4683169689Skan      bool template_p;
4684169689Skan      /* Parse the id-expression.  */
4685169689Skan      name = (cp_parser_id_expression
4686169689Skan	      (parser,
4687169689Skan	       cp_parser_optional_template_keyword (parser),
4688169689Skan	       /*check_dependency_p=*/true,
4689169689Skan	       &template_p,
4690169689Skan	       /*declarator_p=*/false,
4691169689Skan	       /*optional_p=*/false));
4692169689Skan      /* In general, build a SCOPE_REF if the member name is qualified.
4693169689Skan	 However, if the name was not dependent and has already been
4694169689Skan	 resolved; there is no need to build the SCOPE_REF.  For example;
4695169689Skan
4696169689Skan	     struct X { void f(); };
4697169689Skan	     template <typename T> void f(T* t) { t->X::f(); }
4698169689Skan
4699169689Skan	 Even though "t" is dependent, "X::f" is not and has been resolved
4700169689Skan	 to a BASELINK; there is no need to include scope information.  */
4701169689Skan
4702169689Skan      /* But we do need to remember that there was an explicit scope for
4703169689Skan	 virtual function calls.  */
4704169689Skan      if (parser->scope)
4705169689Skan	*idk = CP_ID_KIND_QUALIFIED;
4706169689Skan
4707169689Skan      /* If the name is a template-id that names a type, we will get a
4708169689Skan	 TYPE_DECL here.  That is invalid code.  */
4709169689Skan      if (TREE_CODE (name) == TYPE_DECL)
4710169689Skan	{
4711169689Skan	  error ("invalid use of %qD", name);
4712169689Skan	  postfix_expression = error_mark_node;
4713169689Skan	}
4714169689Skan      else
4715169689Skan	{
4716169689Skan	  if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
4717169689Skan	    {
4718169689Skan	      name = build_qualified_name (/*type=*/NULL_TREE,
4719169689Skan					   parser->scope,
4720169689Skan					   name,
4721169689Skan					   template_p);
4722169689Skan	      parser->scope = NULL_TREE;
4723169689Skan	      parser->qualifying_scope = NULL_TREE;
4724169689Skan	      parser->object_scope = NULL_TREE;
4725169689Skan	    }
4726169689Skan	  if (scope && name && BASELINK_P (name))
4727169689Skan	    adjust_result_of_qualified_name_lookup
4728169689Skan	      (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
4729169689Skan	  postfix_expression
4730169689Skan	    = finish_class_member_access_expr (postfix_expression, name,
4731169689Skan					       template_p);
4732169689Skan	}
4733169689Skan    }
4734169689Skan
4735169689Skan  /* We no longer need to look up names in the scope of the object on
4736169689Skan     the left-hand side of the `.' or `->' operator.  */
4737169689Skan  parser->context->object_type = NULL_TREE;
4738169689Skan
4739169689Skan  /* Outside of offsetof, these operators may not appear in
4740169689Skan     constant-expressions.  */
4741169689Skan  if (!for_offsetof
4742169689Skan      && (cp_parser_non_integral_constant_expression
4743169689Skan	  (parser, token_type == CPP_DEREF ? "'->'" : "`.'")))
4744169689Skan    postfix_expression = error_mark_node;
4745169689Skan
4746169689Skan  return postfix_expression;
4747169689Skan}
4748169689Skan
4749132718Skan/* Parse a parenthesized expression-list.
4750132718Skan
4751132718Skan   expression-list:
4752132718Skan     assignment-expression
4753132718Skan     expression-list, assignment-expression
4754132718Skan
4755132718Skan   attribute-list:
4756132718Skan     expression-list
4757132718Skan     identifier
4758132718Skan     identifier, expression-list
4759132718Skan
4760169689Skan   CAST_P is true if this expression is the target of a cast.
4761169689Skan
4762132718Skan   Returns a TREE_LIST.  The TREE_VALUE of each node is a
4763132718Skan   representation of an assignment-expression.  Note that a TREE_LIST
4764132718Skan   is returned even if there is only a single expression in the list.
4765132718Skan   error_mark_node is returned if the ( and or ) are
4766132718Skan   missing. NULL_TREE is returned on no expressions. The parentheses
4767132718Skan   are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4768132718Skan   list being parsed.  If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4769132718Skan   indicates whether or not all of the expressions in the list were
4770132718Skan   constant.  */
4771132718Skan
4772132718Skanstatic tree
4773169689Skancp_parser_parenthesized_expression_list (cp_parser* parser,
4774132718Skan					 bool is_attribute_list,
4775169689Skan					 bool cast_p,
4776132718Skan					 bool *non_constant_p)
4777132718Skan{
4778132718Skan  tree expression_list = NULL_TREE;
4779169689Skan  bool fold_expr_p = is_attribute_list;
4780132718Skan  tree identifier = NULL_TREE;
4781132718Skan
4782132718Skan  /* Assume all the expressions will be constant.  */
4783132718Skan  if (non_constant_p)
4784132718Skan    *non_constant_p = false;
4785132718Skan
4786132718Skan  if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4787132718Skan    return error_mark_node;
4788169689Skan
4789132718Skan  /* Consume expressions until there are no more.  */
4790132718Skan  if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4791132718Skan    while (true)
4792132718Skan      {
4793132718Skan	tree expr;
4794169689Skan
4795132718Skan	/* At the beginning of attribute lists, check to see if the
4796132718Skan	   next token is an identifier.  */
4797132718Skan	if (is_attribute_list
4798132718Skan	    && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4799132718Skan	  {
4800132718Skan	    cp_token *token;
4801169689Skan
4802132718Skan	    /* Consume the identifier.  */
4803132718Skan	    token = cp_lexer_consume_token (parser->lexer);
4804132718Skan	    /* Save the identifier.  */
4805169689Skan	    identifier = token->u.value;
4806132718Skan	  }
4807132718Skan	else
4808132718Skan	  {
4809132718Skan	    /* Parse the next assignment-expression.  */
4810132718Skan	    if (non_constant_p)
4811132718Skan	      {
4812132718Skan		bool expr_non_constant_p;
4813169689Skan		expr = (cp_parser_constant_expression
4814132718Skan			(parser, /*allow_non_constant_p=*/true,
4815132718Skan			 &expr_non_constant_p));
4816132718Skan		if (expr_non_constant_p)
4817132718Skan		  *non_constant_p = true;
4818132718Skan	      }
4819132718Skan	    else
4820169689Skan	      expr = cp_parser_assignment_expression (parser, cast_p);
4821132718Skan
4822169689Skan	    if (fold_expr_p)
4823169689Skan	      expr = fold_non_dependent_expr (expr);
4824169689Skan
4825132718Skan	     /* Add it to the list.  We add error_mark_node
4826132718Skan		expressions to the list, so that we can still tell if
4827132718Skan		the correct form for a parenthesized expression-list
4828132718Skan		is found. That gives better errors.  */
4829132718Skan	    expression_list = tree_cons (NULL_TREE, expr, expression_list);
4830132718Skan
4831132718Skan	    if (expr == error_mark_node)
4832132718Skan	      goto skip_comma;
4833132718Skan	  }
4834132718Skan
4835132718Skan	/* After the first item, attribute lists look the same as
4836132718Skan	   expression lists.  */
4837132718Skan	is_attribute_list = false;
4838169689Skan
4839132718Skan      get_comma:;
4840132718Skan	/* If the next token isn't a `,', then we are done.  */
4841132718Skan	if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4842132718Skan	  break;
4843132718Skan
4844132718Skan	/* Otherwise, consume the `,' and keep going.  */
4845132718Skan	cp_lexer_consume_token (parser->lexer);
4846132718Skan      }
4847169689Skan
4848132718Skan  if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
4849132718Skan    {
4850132718Skan      int ending;
4851169689Skan
4852132718Skan    skip_comma:;
4853132718Skan      /* We try and resync to an unnested comma, as that will give the
4854132718Skan	 user better diagnostics.  */
4855169689Skan      ending = cp_parser_skip_to_closing_parenthesis (parser,
4856169689Skan						      /*recovering=*/true,
4857132718Skan						      /*or_comma=*/true,
4858132718Skan						      /*consume_paren=*/true);
4859132718Skan      if (ending < 0)
4860132718Skan	goto get_comma;
4861132718Skan      if (!ending)
4862132718Skan	return error_mark_node;
4863132718Skan    }
4864132718Skan
4865132718Skan  /* We built up the list in reverse order so we must reverse it now.  */
4866132718Skan  expression_list = nreverse (expression_list);
4867132718Skan  if (identifier)
4868132718Skan    expression_list = tree_cons (NULL_TREE, identifier, expression_list);
4869169689Skan
4870132718Skan  return expression_list;
4871132718Skan}
4872132718Skan
4873132718Skan/* Parse a pseudo-destructor-name.
4874132718Skan
4875132718Skan   pseudo-destructor-name:
4876132718Skan     :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4877132718Skan     :: [opt] nested-name-specifier template template-id :: ~ type-name
4878132718Skan     :: [opt] nested-name-specifier [opt] ~ type-name
4879132718Skan
4880132718Skan   If either of the first two productions is used, sets *SCOPE to the
4881132718Skan   TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
4882132718Skan   NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
4883132718Skan   or ERROR_MARK_NODE if the parse fails.  */
4884132718Skan
4885132718Skanstatic void
4886169689Skancp_parser_pseudo_destructor_name (cp_parser* parser,
4887169689Skan				  tree* scope,
4888169689Skan				  tree* type)
4889132718Skan{
4890132718Skan  bool nested_name_specifier_p;
4891132718Skan
4892169689Skan  /* Assume that things will not work out.  */
4893169689Skan  *type = error_mark_node;
4894169689Skan
4895132718Skan  /* Look for the optional `::' operator.  */
4896132718Skan  cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4897132718Skan  /* Look for the optional nested-name-specifier.  */
4898169689Skan  nested_name_specifier_p
4899132718Skan    = (cp_parser_nested_name_specifier_opt (parser,
4900132718Skan					    /*typename_keyword_p=*/false,
4901132718Skan					    /*check_dependency_p=*/true,
4902132718Skan					    /*type_p=*/false,
4903169689Skan					    /*is_declaration=*/true)
4904132718Skan       != NULL_TREE);
4905132718Skan  /* Now, if we saw a nested-name-specifier, we might be doing the
4906132718Skan     second production.  */
4907169689Skan  if (nested_name_specifier_p
4908132718Skan      && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4909132718Skan    {
4910132718Skan      /* Consume the `template' keyword.  */
4911132718Skan      cp_lexer_consume_token (parser->lexer);
4912132718Skan      /* Parse the template-id.  */
4913169689Skan      cp_parser_template_id (parser,
4914132718Skan			     /*template_keyword_p=*/true,
4915132718Skan			     /*check_dependency_p=*/false,
4916132718Skan			     /*is_declaration=*/true);
4917132718Skan      /* Look for the `::' token.  */
4918132718Skan      cp_parser_require (parser, CPP_SCOPE, "`::'");
4919132718Skan    }
4920132718Skan  /* If the next token is not a `~', then there might be some
4921132718Skan     additional qualification.  */
4922132718Skan  else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4923132718Skan    {
4924132718Skan      /* Look for the type-name.  */
4925132718Skan      *scope = TREE_TYPE (cp_parser_type_name (parser));
4926132718Skan
4927169689Skan      if (*scope == error_mark_node)
4928169689Skan	return;
4929169689Skan
4930169689Skan      /* If we don't have ::~, then something has gone wrong.  Since
4931169689Skan	 the only caller of this function is looking for something
4932169689Skan	 after `.' or `->' after a scalar type, most likely the
4933169689Skan	 program is trying to get a member of a non-aggregate
4934169689Skan	 type.  */
4935169689Skan      if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
4936132718Skan	  || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
4937132718Skan	{
4938132718Skan	  cp_parser_error (parser, "request for member of non-aggregate type");
4939132718Skan	  return;
4940132718Skan	}
4941132718Skan
4942132718Skan      /* Look for the `::' token.  */
4943132718Skan      cp_parser_require (parser, CPP_SCOPE, "`::'");
4944132718Skan    }
4945132718Skan  else
4946132718Skan    *scope = NULL_TREE;
4947132718Skan
4948132718Skan  /* Look for the `~'.  */
4949132718Skan  cp_parser_require (parser, CPP_COMPL, "`~'");
4950132718Skan  /* Look for the type-name again.  We are not responsible for
4951132718Skan     checking that it matches the first type-name.  */
4952132718Skan  *type = cp_parser_type_name (parser);
4953132718Skan}
4954132718Skan
4955132718Skan/* Parse a unary-expression.
4956132718Skan
4957132718Skan   unary-expression:
4958132718Skan     postfix-expression
4959132718Skan     ++ cast-expression
4960132718Skan     -- cast-expression
4961132718Skan     unary-operator cast-expression
4962132718Skan     sizeof unary-expression
4963132718Skan     sizeof ( type-id )
4964132718Skan     new-expression
4965132718Skan     delete-expression
4966132718Skan
4967132718Skan   GNU Extensions:
4968132718Skan
4969132718Skan   unary-expression:
4970132718Skan     __extension__ cast-expression
4971132718Skan     __alignof__ unary-expression
4972132718Skan     __alignof__ ( type-id )
4973132718Skan     __real__ cast-expression
4974132718Skan     __imag__ cast-expression
4975132718Skan     && identifier
4976132718Skan
4977132718Skan   ADDRESS_P is true iff the unary-expression is appearing as the
4978169689Skan   operand of the `&' operator.   CAST_P is true if this expression is
4979169689Skan   the target of a cast.
4980132718Skan
4981132718Skan   Returns a representation of the expression.  */
4982132718Skan
4983132718Skanstatic tree
4984169689Skancp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p)
4985132718Skan{
4986132718Skan  cp_token *token;
4987132718Skan  enum tree_code unary_operator;
4988132718Skan
4989132718Skan  /* Peek at the next token.  */
4990132718Skan  token = cp_lexer_peek_token (parser->lexer);
4991132718Skan  /* Some keywords give away the kind of expression.  */
4992132718Skan  if (token->type == CPP_KEYWORD)
4993132718Skan    {
4994132718Skan      enum rid keyword = token->keyword;
4995132718Skan
4996132718Skan      switch (keyword)
4997132718Skan	{
4998132718Skan	case RID_ALIGNOF:
4999132718Skan	case RID_SIZEOF:
5000132718Skan	  {
5001132718Skan	    tree operand;
5002132718Skan	    enum tree_code op;
5003169689Skan
5004132718Skan	    op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5005132718Skan	    /* Consume the token.  */
5006132718Skan	    cp_lexer_consume_token (parser->lexer);
5007132718Skan	    /* Parse the operand.  */
5008132718Skan	    operand = cp_parser_sizeof_operand (parser, keyword);
5009132718Skan
5010132718Skan	    if (TYPE_P (operand))
5011132718Skan	      return cxx_sizeof_or_alignof_type (operand, op, true);
5012132718Skan	    else
5013132718Skan	      return cxx_sizeof_or_alignof_expr (operand, op);
5014132718Skan	  }
5015132718Skan
5016132718Skan	case RID_NEW:
5017132718Skan	  return cp_parser_new_expression (parser);
5018132718Skan
5019132718Skan	case RID_DELETE:
5020132718Skan	  return cp_parser_delete_expression (parser);
5021169689Skan
5022132718Skan	case RID_EXTENSION:
5023132718Skan	  {
5024132718Skan	    /* The saved value of the PEDANTIC flag.  */
5025132718Skan	    int saved_pedantic;
5026132718Skan	    tree expr;
5027132718Skan
5028132718Skan	    /* Save away the PEDANTIC flag.  */
5029132718Skan	    cp_parser_extension_opt (parser, &saved_pedantic);
5030132718Skan	    /* Parse the cast-expression.  */
5031132718Skan	    expr = cp_parser_simple_cast_expression (parser);
5032132718Skan	    /* Restore the PEDANTIC flag.  */
5033132718Skan	    pedantic = saved_pedantic;
5034132718Skan
5035132718Skan	    return expr;
5036132718Skan	  }
5037132718Skan
5038132718Skan	case RID_REALPART:
5039132718Skan	case RID_IMAGPART:
5040132718Skan	  {
5041132718Skan	    tree expression;
5042132718Skan
5043132718Skan	    /* Consume the `__real__' or `__imag__' token.  */
5044132718Skan	    cp_lexer_consume_token (parser->lexer);
5045132718Skan	    /* Parse the cast-expression.  */
5046132718Skan	    expression = cp_parser_simple_cast_expression (parser);
5047132718Skan	    /* Create the complete representation.  */
5048132718Skan	    return build_x_unary_op ((keyword == RID_REALPART
5049132718Skan				      ? REALPART_EXPR : IMAGPART_EXPR),
5050132718Skan				     expression);
5051132718Skan	  }
5052132718Skan	  break;
5053132718Skan
5054132718Skan	default:
5055132718Skan	  break;
5056132718Skan	}
5057132718Skan    }
5058132718Skan
5059132718Skan  /* Look for the `:: new' and `:: delete', which also signal the
5060132718Skan     beginning of a new-expression, or delete-expression,
5061132718Skan     respectively.  If the next token is `::', then it might be one of
5062132718Skan     these.  */
5063132718Skan  if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5064132718Skan    {
5065132718Skan      enum rid keyword;
5066132718Skan
5067132718Skan      /* See if the token after the `::' is one of the keywords in
5068132718Skan	 which we're interested.  */
5069132718Skan      keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5070132718Skan      /* If it's `new', we have a new-expression.  */
5071132718Skan      if (keyword == RID_NEW)
5072132718Skan	return cp_parser_new_expression (parser);
5073132718Skan      /* Similarly, for `delete'.  */
5074132718Skan      else if (keyword == RID_DELETE)
5075132718Skan	return cp_parser_delete_expression (parser);
5076132718Skan    }
5077132718Skan
5078132718Skan  /* Look for a unary operator.  */
5079132718Skan  unary_operator = cp_parser_unary_operator (token);
5080132718Skan  /* The `++' and `--' operators can be handled similarly, even though
5081132718Skan     they are not technically unary-operators in the grammar.  */
5082132718Skan  if (unary_operator == ERROR_MARK)
5083132718Skan    {
5084132718Skan      if (token->type == CPP_PLUS_PLUS)
5085132718Skan	unary_operator = PREINCREMENT_EXPR;
5086132718Skan      else if (token->type == CPP_MINUS_MINUS)
5087132718Skan	unary_operator = PREDECREMENT_EXPR;
5088132718Skan      /* Handle the GNU address-of-label extension.  */
5089132718Skan      else if (cp_parser_allow_gnu_extensions_p (parser)
5090132718Skan	       && token->type == CPP_AND_AND)
5091132718Skan	{
5092132718Skan	  tree identifier;
5093132718Skan
5094132718Skan	  /* Consume the '&&' token.  */
5095132718Skan	  cp_lexer_consume_token (parser->lexer);
5096132718Skan	  /* Look for the identifier.  */
5097132718Skan	  identifier = cp_parser_identifier (parser);
5098132718Skan	  /* Create an expression representing the address.  */
5099132718Skan	  return finish_label_address_expr (identifier);
5100132718Skan	}
5101132718Skan    }
5102132718Skan  if (unary_operator != ERROR_MARK)
5103132718Skan    {
5104132718Skan      tree cast_expression;
5105132718Skan      tree expression = error_mark_node;
5106132718Skan      const char *non_constant_p = NULL;
5107132718Skan
5108132718Skan      /* Consume the operator token.  */
5109132718Skan      token = cp_lexer_consume_token (parser->lexer);
5110132718Skan      /* Parse the cast-expression.  */
5111169689Skan      cast_expression
5112169689Skan	= cp_parser_cast_expression (parser,
5113169689Skan				     unary_operator == ADDR_EXPR,
5114169689Skan				     /*cast_p=*/false);
5115132718Skan      /* Now, build an appropriate representation.  */
5116132718Skan      switch (unary_operator)
5117132718Skan	{
5118132718Skan	case INDIRECT_REF:
5119132718Skan	  non_constant_p = "`*'";
5120132718Skan	  expression = build_x_indirect_ref (cast_expression, "unary *");
5121132718Skan	  break;
5122132718Skan
5123132718Skan	case ADDR_EXPR:
5124169689Skan	  non_constant_p = "`&'";
5125132718Skan	  /* Fall through.  */
5126132718Skan	case BIT_NOT_EXPR:
5127132718Skan	  expression = build_x_unary_op (unary_operator, cast_expression);
5128132718Skan	  break;
5129132718Skan
5130132718Skan	case PREINCREMENT_EXPR:
5131132718Skan	case PREDECREMENT_EXPR:
5132132718Skan	  non_constant_p = (unary_operator == PREINCREMENT_EXPR
5133132718Skan			    ? "`++'" : "`--'");
5134132718Skan	  /* Fall through.  */
5135169689Skan	case UNARY_PLUS_EXPR:
5136132718Skan	case NEGATE_EXPR:
5137132718Skan	case TRUTH_NOT_EXPR:
5138132718Skan	  expression = finish_unary_op_expr (unary_operator, cast_expression);
5139132718Skan	  break;
5140132718Skan
5141132718Skan	default:
5142169689Skan	  gcc_unreachable ();
5143132718Skan	}
5144132718Skan
5145169689Skan      if (non_constant_p
5146132718Skan	  && cp_parser_non_integral_constant_expression (parser,
5147132718Skan							 non_constant_p))
5148132718Skan	expression = error_mark_node;
5149132718Skan
5150132718Skan      return expression;
5151132718Skan    }
5152132718Skan
5153169689Skan  return cp_parser_postfix_expression (parser, address_p, cast_p);
5154132718Skan}
5155132718Skan
5156132718Skan/* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
5157132718Skan   unary-operator, the corresponding tree code is returned.  */
5158132718Skan
5159132718Skanstatic enum tree_code
5160132718Skancp_parser_unary_operator (cp_token* token)
5161132718Skan{
5162132718Skan  switch (token->type)
5163132718Skan    {
5164132718Skan    case CPP_MULT:
5165132718Skan      return INDIRECT_REF;
5166132718Skan
5167132718Skan    case CPP_AND:
5168132718Skan      return ADDR_EXPR;
5169132718Skan
5170132718Skan    case CPP_PLUS:
5171169689Skan      return UNARY_PLUS_EXPR;
5172132718Skan
5173132718Skan    case CPP_MINUS:
5174132718Skan      return NEGATE_EXPR;
5175132718Skan
5176132718Skan    case CPP_NOT:
5177132718Skan      return TRUTH_NOT_EXPR;
5178169689Skan
5179132718Skan    case CPP_COMPL:
5180132718Skan      return BIT_NOT_EXPR;
5181132718Skan
5182132718Skan    default:
5183132718Skan      return ERROR_MARK;
5184132718Skan    }
5185132718Skan}
5186132718Skan
5187132718Skan/* Parse a new-expression.
5188132718Skan
5189132718Skan   new-expression:
5190132718Skan     :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5191132718Skan     :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5192132718Skan
5193132718Skan   Returns a representation of the expression.  */
5194132718Skan
5195132718Skanstatic tree
5196132718Skancp_parser_new_expression (cp_parser* parser)
5197132718Skan{
5198132718Skan  bool global_scope_p;
5199132718Skan  tree placement;
5200132718Skan  tree type;
5201132718Skan  tree initializer;
5202169689Skan  tree nelts;
5203132718Skan
5204132718Skan  /* Look for the optional `::' operator.  */
5205169689Skan  global_scope_p
5206132718Skan    = (cp_parser_global_scope_opt (parser,
5207132718Skan				   /*current_scope_valid_p=*/false)
5208132718Skan       != NULL_TREE);
5209132718Skan  /* Look for the `new' operator.  */
5210132718Skan  cp_parser_require_keyword (parser, RID_NEW, "`new'");
5211132718Skan  /* There's no easy way to tell a new-placement from the
5212132718Skan     `( type-id )' construct.  */
5213132718Skan  cp_parser_parse_tentatively (parser);
5214132718Skan  /* Look for a new-placement.  */
5215132718Skan  placement = cp_parser_new_placement (parser);
5216132718Skan  /* If that didn't work out, there's no new-placement.  */
5217132718Skan  if (!cp_parser_parse_definitely (parser))
5218132718Skan    placement = NULL_TREE;
5219132718Skan
5220132718Skan  /* If the next token is a `(', then we have a parenthesized
5221132718Skan     type-id.  */
5222132718Skan  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5223132718Skan    {
5224132718Skan      /* Consume the `('.  */
5225132718Skan      cp_lexer_consume_token (parser->lexer);
5226132718Skan      /* Parse the type-id.  */
5227132718Skan      type = cp_parser_type_id (parser);
5228132718Skan      /* Look for the closing `)'.  */
5229132718Skan      cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5230169689Skan      /* There should not be a direct-new-declarator in this production,
5231169689Skan	 but GCC used to allowed this, so we check and emit a sensible error
5232132718Skan	 message for this case.  */
5233132718Skan      if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5234132718Skan	{
5235132718Skan	  error ("array bound forbidden after parenthesized type-id");
5236132718Skan	  inform ("try removing the parentheses around the type-id");
5237132718Skan	  cp_parser_direct_new_declarator (parser);
5238132718Skan	}
5239169689Skan      nelts = NULL_TREE;
5240132718Skan    }
5241132718Skan  /* Otherwise, there must be a new-type-id.  */
5242132718Skan  else
5243169689Skan    type = cp_parser_new_type_id (parser, &nelts);
5244132718Skan
5245132718Skan  /* If the next token is a `(', then we have a new-initializer.  */
5246132718Skan  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5247132718Skan    initializer = cp_parser_new_initializer (parser);
5248132718Skan  else
5249132718Skan    initializer = NULL_TREE;
5250132718Skan
5251132718Skan  /* A new-expression may not appear in an integral constant
5252132718Skan     expression.  */
5253132718Skan  if (cp_parser_non_integral_constant_expression (parser, "`new'"))
5254132718Skan    return error_mark_node;
5255132718Skan
5256132718Skan  /* Create a representation of the new-expression.  */
5257169689Skan  return build_new (placement, type, nelts, initializer, global_scope_p);
5258132718Skan}
5259132718Skan
5260132718Skan/* Parse a new-placement.
5261132718Skan
5262132718Skan   new-placement:
5263132718Skan     ( expression-list )
5264132718Skan
5265132718Skan   Returns the same representation as for an expression-list.  */
5266132718Skan
5267132718Skanstatic tree
5268132718Skancp_parser_new_placement (cp_parser* parser)
5269132718Skan{
5270132718Skan  tree expression_list;
5271132718Skan
5272132718Skan  /* Parse the expression-list.  */
5273169689Skan  expression_list = (cp_parser_parenthesized_expression_list
5274169689Skan		     (parser, false, /*cast_p=*/false,
5275169689Skan		      /*non_constant_p=*/NULL));
5276132718Skan
5277132718Skan  return expression_list;
5278132718Skan}
5279132718Skan
5280132718Skan/* Parse a new-type-id.
5281132718Skan
5282132718Skan   new-type-id:
5283132718Skan     type-specifier-seq new-declarator [opt]
5284132718Skan
5285169689Skan   Returns the TYPE allocated.  If the new-type-id indicates an array
5286169689Skan   type, *NELTS is set to the number of elements in the last array
5287169689Skan   bound; the TYPE will not include the last array bound.  */
5288132718Skan
5289132718Skanstatic tree
5290169689Skancp_parser_new_type_id (cp_parser* parser, tree *nelts)
5291132718Skan{
5292169689Skan  cp_decl_specifier_seq type_specifier_seq;
5293169689Skan  cp_declarator *new_declarator;
5294169689Skan  cp_declarator *declarator;
5295169689Skan  cp_declarator *outer_declarator;
5296132718Skan  const char *saved_message;
5297169689Skan  tree type;
5298132718Skan
5299132718Skan  /* The type-specifier sequence must not contain type definitions.
5300132718Skan     (It cannot contain declarations of new types either, but if they
5301132718Skan     are not definitions we will catch that because they are not
5302132718Skan     complete.)  */
5303132718Skan  saved_message = parser->type_definition_forbidden_message;
5304132718Skan  parser->type_definition_forbidden_message
5305132718Skan    = "types may not be defined in a new-type-id";
5306132718Skan  /* Parse the type-specifier-seq.  */
5307169689Skan  cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5308169689Skan				&type_specifier_seq);
5309132718Skan  /* Restore the old message.  */
5310132718Skan  parser->type_definition_forbidden_message = saved_message;
5311132718Skan  /* Parse the new-declarator.  */
5312169689Skan  new_declarator = cp_parser_new_declarator_opt (parser);
5313132718Skan
5314169689Skan  /* Determine the number of elements in the last array dimension, if
5315169689Skan     any.  */
5316169689Skan  *nelts = NULL_TREE;
5317169689Skan  /* Skip down to the last array dimension.  */
5318169689Skan  declarator = new_declarator;
5319169689Skan  outer_declarator = NULL;
5320169689Skan  while (declarator && (declarator->kind == cdk_pointer
5321169689Skan			|| declarator->kind == cdk_ptrmem))
5322169689Skan    {
5323169689Skan      outer_declarator = declarator;
5324169689Skan      declarator = declarator->declarator;
5325169689Skan    }
5326169689Skan  while (declarator
5327169689Skan	 && declarator->kind == cdk_array
5328169689Skan	 && declarator->declarator
5329169689Skan	 && declarator->declarator->kind == cdk_array)
5330169689Skan    {
5331169689Skan      outer_declarator = declarator;
5332169689Skan      declarator = declarator->declarator;
5333169689Skan    }
5334169689Skan
5335169689Skan  if (declarator && declarator->kind == cdk_array)
5336169689Skan    {
5337169689Skan      *nelts = declarator->u.array.bounds;
5338169689Skan      if (*nelts == error_mark_node)
5339169689Skan	*nelts = integer_one_node;
5340169689Skan
5341169689Skan      if (outer_declarator)
5342169689Skan	outer_declarator->declarator = declarator->declarator;
5343169689Skan      else
5344169689Skan	new_declarator = NULL;
5345169689Skan    }
5346169689Skan
5347169689Skan  type = groktypename (&type_specifier_seq, new_declarator);
5348169689Skan  if (TREE_CODE (type) == ARRAY_TYPE && *nelts == NULL_TREE)
5349169689Skan    {
5350169689Skan      *nelts = array_type_nelts_top (type);
5351169689Skan      type = TREE_TYPE (type);
5352169689Skan    }
5353169689Skan  return type;
5354132718Skan}
5355132718Skan
5356132718Skan/* Parse an (optional) new-declarator.
5357132718Skan
5358132718Skan   new-declarator:
5359132718Skan     ptr-operator new-declarator [opt]
5360132718Skan     direct-new-declarator
5361132718Skan
5362169689Skan   Returns the declarator.  */
5363132718Skan
5364169689Skanstatic cp_declarator *
5365132718Skancp_parser_new_declarator_opt (cp_parser* parser)
5366132718Skan{
5367132718Skan  enum tree_code code;
5368132718Skan  tree type;
5369169689Skan  cp_cv_quals cv_quals;
5370132718Skan
5371132718Skan  /* We don't know if there's a ptr-operator next, or not.  */
5372132718Skan  cp_parser_parse_tentatively (parser);
5373132718Skan  /* Look for a ptr-operator.  */
5374169689Skan  code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5375132718Skan  /* If that worked, look for more new-declarators.  */
5376132718Skan  if (cp_parser_parse_definitely (parser))
5377132718Skan    {
5378169689Skan      cp_declarator *declarator;
5379132718Skan
5380132718Skan      /* Parse another optional declarator.  */
5381132718Skan      declarator = cp_parser_new_declarator_opt (parser);
5382132718Skan
5383132718Skan      /* Create the representation of the declarator.  */
5384169689Skan      if (type)
5385169689Skan	declarator = make_ptrmem_declarator (cv_quals, type, declarator);
5386169689Skan      else if (code == INDIRECT_REF)
5387169689Skan	declarator = make_pointer_declarator (cv_quals, declarator);
5388132718Skan      else
5389169689Skan	declarator = make_reference_declarator (cv_quals, declarator);
5390132718Skan
5391132718Skan      return declarator;
5392132718Skan    }
5393132718Skan
5394132718Skan  /* If the next token is a `[', there is a direct-new-declarator.  */
5395132718Skan  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5396132718Skan    return cp_parser_direct_new_declarator (parser);
5397132718Skan
5398169689Skan  return NULL;
5399132718Skan}
5400132718Skan
5401132718Skan/* Parse a direct-new-declarator.
5402132718Skan
5403132718Skan   direct-new-declarator:
5404132718Skan     [ expression ]
5405169689Skan     direct-new-declarator [constant-expression]
5406132718Skan
5407169689Skan   */
5408132718Skan
5409169689Skanstatic cp_declarator *
5410132718Skancp_parser_direct_new_declarator (cp_parser* parser)
5411132718Skan{
5412169689Skan  cp_declarator *declarator = NULL;
5413132718Skan
5414132718Skan  while (true)
5415132718Skan    {
5416132718Skan      tree expression;
5417132718Skan
5418132718Skan      /* Look for the opening `['.  */
5419132718Skan      cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
5420132718Skan      /* The first expression is not required to be constant.  */
5421132718Skan      if (!declarator)
5422132718Skan	{
5423169689Skan	  expression = cp_parser_expression (parser, /*cast_p=*/false);
5424132718Skan	  /* The standard requires that the expression have integral
5425132718Skan	     type.  DR 74 adds enumeration types.  We believe that the
5426132718Skan	     real intent is that these expressions be handled like the
5427132718Skan	     expression in a `switch' condition, which also allows
5428132718Skan	     classes with a single conversion to integral or
5429132718Skan	     enumeration type.  */
5430132718Skan	  if (!processing_template_decl)
5431132718Skan	    {
5432169689Skan	      expression
5433132718Skan		= build_expr_type_conversion (WANT_INT | WANT_ENUM,
5434132718Skan					      expression,
5435132718Skan					      /*complain=*/true);
5436132718Skan	      if (!expression)
5437132718Skan		{
5438169689Skan		  error ("expression in new-declarator must have integral "
5439169689Skan			 "or enumeration type");
5440132718Skan		  expression = error_mark_node;
5441132718Skan		}
5442132718Skan	    }
5443132718Skan	}
5444132718Skan      /* But all the other expressions must be.  */
5445132718Skan      else
5446169689Skan	expression
5447169689Skan	  = cp_parser_constant_expression (parser,
5448132718Skan					   /*allow_non_constant=*/false,
5449132718Skan					   NULL);
5450132718Skan      /* Look for the closing `]'.  */
5451132718Skan      cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5452132718Skan
5453132718Skan      /* Add this bound to the declarator.  */
5454169689Skan      declarator = make_array_declarator (declarator, expression);
5455132718Skan
5456132718Skan      /* If the next token is not a `[', then there are no more
5457132718Skan	 bounds.  */
5458132718Skan      if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5459132718Skan	break;
5460132718Skan    }
5461132718Skan
5462132718Skan  return declarator;
5463132718Skan}
5464132718Skan
5465132718Skan/* Parse a new-initializer.
5466132718Skan
5467132718Skan   new-initializer:
5468132718Skan     ( expression-list [opt] )
5469132718Skan
5470132718Skan   Returns a representation of the expression-list.  If there is no
5471132718Skan   expression-list, VOID_ZERO_NODE is returned.  */
5472132718Skan
5473132718Skanstatic tree
5474132718Skancp_parser_new_initializer (cp_parser* parser)
5475132718Skan{
5476132718Skan  tree expression_list;
5477132718Skan
5478169689Skan  expression_list = (cp_parser_parenthesized_expression_list
5479169689Skan		     (parser, false, /*cast_p=*/false,
5480169689Skan		      /*non_constant_p=*/NULL));
5481132718Skan  if (!expression_list)
5482132718Skan    expression_list = void_zero_node;
5483132718Skan
5484132718Skan  return expression_list;
5485132718Skan}
5486132718Skan
5487132718Skan/* Parse a delete-expression.
5488132718Skan
5489132718Skan   delete-expression:
5490132718Skan     :: [opt] delete cast-expression
5491132718Skan     :: [opt] delete [ ] cast-expression
5492132718Skan
5493132718Skan   Returns a representation of the expression.  */
5494132718Skan
5495132718Skanstatic tree
5496132718Skancp_parser_delete_expression (cp_parser* parser)
5497132718Skan{
5498132718Skan  bool global_scope_p;
5499132718Skan  bool array_p;
5500132718Skan  tree expression;
5501132718Skan
5502132718Skan  /* Look for the optional `::' operator.  */
5503132718Skan  global_scope_p
5504132718Skan    = (cp_parser_global_scope_opt (parser,
5505132718Skan				   /*current_scope_valid_p=*/false)
5506132718Skan       != NULL_TREE);
5507132718Skan  /* Look for the `delete' keyword.  */
5508132718Skan  cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
5509132718Skan  /* See if the array syntax is in use.  */
5510132718Skan  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5511132718Skan    {
5512132718Skan      /* Consume the `[' token.  */
5513132718Skan      cp_lexer_consume_token (parser->lexer);
5514132718Skan      /* Look for the `]' token.  */
5515132718Skan      cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5516132718Skan      /* Remember that this is the `[]' construct.  */
5517132718Skan      array_p = true;
5518132718Skan    }
5519132718Skan  else
5520132718Skan    array_p = false;
5521132718Skan
5522132718Skan  /* Parse the cast-expression.  */
5523132718Skan  expression = cp_parser_simple_cast_expression (parser);
5524132718Skan
5525132718Skan  /* A delete-expression may not appear in an integral constant
5526132718Skan     expression.  */
5527132718Skan  if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
5528132718Skan    return error_mark_node;
5529132718Skan
5530132718Skan  return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5531132718Skan}
5532132718Skan
5533132718Skan/* Parse a cast-expression.
5534132718Skan
5535132718Skan   cast-expression:
5536132718Skan     unary-expression
5537132718Skan     ( type-id ) cast-expression
5538132718Skan
5539169689Skan   ADDRESS_P is true iff the unary-expression is appearing as the
5540169689Skan   operand of the `&' operator.   CAST_P is true if this expression is
5541169689Skan   the target of a cast.
5542169689Skan
5543132718Skan   Returns a representation of the expression.  */
5544132718Skan
5545132718Skanstatic tree
5546169689Skancp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p)
5547132718Skan{
5548132718Skan  /* If it's a `(', then we might be looking at a cast.  */
5549132718Skan  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5550132718Skan    {
5551132718Skan      tree type = NULL_TREE;
5552132718Skan      tree expr = NULL_TREE;
5553132718Skan      bool compound_literal_p;
5554132718Skan      const char *saved_message;
5555132718Skan
5556132718Skan      /* There's no way to know yet whether or not this is a cast.
5557132718Skan	 For example, `(int (3))' is a unary-expression, while `(int)
5558132718Skan	 3' is a cast.  So, we resort to parsing tentatively.  */
5559132718Skan      cp_parser_parse_tentatively (parser);
5560132718Skan      /* Types may not be defined in a cast.  */
5561132718Skan      saved_message = parser->type_definition_forbidden_message;
5562132718Skan      parser->type_definition_forbidden_message
5563132718Skan	= "types may not be defined in casts";
5564132718Skan      /* Consume the `('.  */
5565132718Skan      cp_lexer_consume_token (parser->lexer);
5566132718Skan      /* A very tricky bit is that `(struct S) { 3 }' is a
5567132718Skan	 compound-literal (which we permit in C++ as an extension).
5568132718Skan	 But, that construct is not a cast-expression -- it is a
5569132718Skan	 postfix-expression.  (The reason is that `(struct S) { 3 }.i'
5570132718Skan	 is legal; if the compound-literal were a cast-expression,
5571132718Skan	 you'd need an extra set of parentheses.)  But, if we parse
5572132718Skan	 the type-id, and it happens to be a class-specifier, then we
5573132718Skan	 will commit to the parse at that point, because we cannot
5574132718Skan	 undo the action that is done when creating a new class.  So,
5575169689Skan	 then we cannot back up and do a postfix-expression.
5576132718Skan
5577132718Skan	 Therefore, we scan ahead to the closing `)', and check to see
5578132718Skan	 if the token after the `)' is a `{'.  If so, we are not
5579169689Skan	 looking at a cast-expression.
5580132718Skan
5581132718Skan	 Save tokens so that we can put them back.  */
5582132718Skan      cp_lexer_save_tokens (parser->lexer);
5583132718Skan      /* Skip tokens until the next token is a closing parenthesis.
5584132718Skan	 If we find the closing `)', and the next token is a `{', then
5585132718Skan	 we are looking at a compound-literal.  */
5586169689Skan      compound_literal_p
5587132718Skan	= (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5588132718Skan						  /*consume_paren=*/true)
5589132718Skan	   && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5590132718Skan      /* Roll back the tokens we skipped.  */
5591132718Skan      cp_lexer_rollback_tokens (parser->lexer);
5592132718Skan      /* If we were looking at a compound-literal, simulate an error
5593132718Skan	 so that the call to cp_parser_parse_definitely below will
5594132718Skan	 fail.  */
5595132718Skan      if (compound_literal_p)
5596132718Skan	cp_parser_simulate_error (parser);
5597132718Skan      else
5598132718Skan	{
5599132718Skan	  bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5600132718Skan	  parser->in_type_id_in_expr_p = true;
5601132718Skan	  /* Look for the type-id.  */
5602132718Skan	  type = cp_parser_type_id (parser);
5603132718Skan	  /* Look for the closing `)'.  */
5604132718Skan	  cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5605132718Skan	  parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5606132718Skan	}
5607132718Skan
5608132718Skan      /* Restore the saved message.  */
5609132718Skan      parser->type_definition_forbidden_message = saved_message;
5610132718Skan
5611132718Skan      /* If ok so far, parse the dependent expression. We cannot be
5612169689Skan	 sure it is a cast. Consider `(T ())'.  It is a parenthesized
5613169689Skan	 ctor of T, but looks like a cast to function returning T
5614169689Skan	 without a dependent expression.  */
5615132718Skan      if (!cp_parser_error_occurred (parser))
5616169689Skan	expr = cp_parser_cast_expression (parser,
5617169689Skan					  /*address_p=*/false,
5618169689Skan					  /*cast_p=*/true);
5619132718Skan
5620132718Skan      if (cp_parser_parse_definitely (parser))
5621132718Skan	{
5622132718Skan	  /* Warn about old-style casts, if so requested.  */
5623169689Skan	  if (warn_old_style_cast
5624169689Skan	      && !in_system_header
5625169689Skan	      && !VOID_TYPE_P (type)
5626132718Skan	      && current_lang_name != lang_name_c)
5627169689Skan	    warning (OPT_Wold_style_cast, "use of old-style cast");
5628132718Skan
5629132718Skan	  /* Only type conversions to integral or enumeration types
5630132718Skan	     can be used in constant-expressions.  */
5631169689Skan	  if (!cast_valid_in_integral_constant_expression_p (type)
5632169689Skan	      && (cp_parser_non_integral_constant_expression
5633132718Skan		  (parser,
5634169689Skan		   "a cast to a type other than an integral or "
5635132718Skan		   "enumeration type")))
5636132718Skan	    return error_mark_node;
5637132718Skan
5638132718Skan	  /* Perform the cast.  */
5639132718Skan	  expr = build_c_cast (type, expr);
5640132718Skan	  return expr;
5641132718Skan	}
5642132718Skan    }
5643132718Skan
5644132718Skan  /* If we get here, then it's not a cast, so it must be a
5645132718Skan     unary-expression.  */
5646169689Skan  return cp_parser_unary_expression (parser, address_p, cast_p);
5647132718Skan}
5648132718Skan
5649169689Skan/* Parse a binary expression of the general form:
5650132718Skan
5651132718Skan   pm-expression:
5652132718Skan     cast-expression
5653132718Skan     pm-expression .* cast-expression
5654132718Skan     pm-expression ->* cast-expression
5655132718Skan
5656169689Skan   multiplicative-expression:
5657132718Skan     pm-expression
5658132718Skan     multiplicative-expression * pm-expression
5659132718Skan     multiplicative-expression / pm-expression
5660132718Skan     multiplicative-expression % pm-expression
5661132718Skan
5662132718Skan   additive-expression:
5663132718Skan     multiplicative-expression
5664132718Skan     additive-expression + multiplicative-expression
5665132718Skan     additive-expression - multiplicative-expression
5666132718Skan
5667132718Skan   shift-expression:
5668132718Skan     additive-expression
5669132718Skan     shift-expression << additive-expression
5670132718Skan     shift-expression >> additive-expression
5671132718Skan
5672132718Skan   relational-expression:
5673132718Skan     shift-expression
5674132718Skan     relational-expression < shift-expression
5675132718Skan     relational-expression > shift-expression
5676132718Skan     relational-expression <= shift-expression
5677132718Skan     relational-expression >= shift-expression
5678132718Skan
5679169689Skan  GNU Extension:
5680132718Skan
5681132718Skan   relational-expression:
5682132718Skan     relational-expression <? shift-expression
5683132718Skan     relational-expression >? shift-expression
5684132718Skan
5685132718Skan   equality-expression:
5686132718Skan     relational-expression
5687132718Skan     equality-expression == relational-expression
5688132718Skan     equality-expression != relational-expression
5689132718Skan
5690132718Skan   and-expression:
5691132718Skan     equality-expression
5692132718Skan     and-expression & equality-expression
5693132718Skan
5694132718Skan   exclusive-or-expression:
5695132718Skan     and-expression
5696132718Skan     exclusive-or-expression ^ and-expression
5697132718Skan
5698169689Skan   inclusive-or-expression:
5699169689Skan     exclusive-or-expression
5700169689Skan     inclusive-or-expression | exclusive-or-expression
5701132718Skan
5702169689Skan   logical-and-expression:
5703169689Skan     inclusive-or-expression
5704169689Skan     logical-and-expression && inclusive-or-expression
5705132718Skan
5706169689Skan   logical-or-expression:
5707169689Skan     logical-and-expression
5708169689Skan     logical-or-expression || logical-and-expression
5709132718Skan
5710169689Skan   All these are implemented with a single function like:
5711132718Skan
5712169689Skan   binary-expression:
5713169689Skan     simple-cast-expression
5714169689Skan     binary-expression <token> binary-expression
5715132718Skan
5716169689Skan   CAST_P is true if this expression is the target of a cast.
5717132718Skan
5718169689Skan   The binops_by_token map is used to get the tree codes for each <token> type.
5719169689Skan   binary-expressions are associated according to a precedence table.  */
5720132718Skan
5721169689Skan#define TOKEN_PRECEDENCE(token) \
5722169689Skan  ((token->type == CPP_GREATER && !parser->greater_than_is_operator_p) \
5723169689Skan   ? PREC_NOT_OPERATOR \
5724169689Skan   : binops_by_token[token->type].prec)
5725169689Skan
5726132718Skanstatic tree
5727169689Skancp_parser_binary_expression (cp_parser* parser, bool cast_p)
5728132718Skan{
5729169689Skan  cp_parser_expression_stack stack;
5730169689Skan  cp_parser_expression_stack_entry *sp = &stack[0];
5731169689Skan  tree lhs, rhs;
5732169689Skan  cp_token *token;
5733169689Skan  enum tree_code tree_type;
5734169689Skan  enum cp_parser_prec prec = PREC_NOT_OPERATOR, new_prec, lookahead_prec;
5735169689Skan  bool overloaded_p;
5736132718Skan
5737169689Skan  /* Parse the first expression.  */
5738169689Skan  lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p);
5739132718Skan
5740169689Skan  for (;;)
5741169689Skan    {
5742169689Skan      /* Get an operator token.  */
5743169689Skan      token = cp_lexer_peek_token (parser->lexer);
5744132718Skan
5745169689Skan      new_prec = TOKEN_PRECEDENCE (token);
5746132718Skan
5747169689Skan      /* Popping an entry off the stack means we completed a subexpression:
5748169689Skan	 - either we found a token which is not an operator (`>' where it is not
5749169689Skan	   an operator, or prec == PREC_NOT_OPERATOR), in which case popping
5750169689Skan	   will happen repeatedly;
5751169689Skan	 - or, we found an operator which has lower priority.  This is the case
5752169689Skan	   where the recursive descent *ascends*, as in `3 * 4 + 5' after
5753169689Skan	   parsing `3 * 4'.  */
5754169689Skan      if (new_prec <= prec)
5755169689Skan	{
5756169689Skan	  if (sp == stack)
5757169689Skan	    break;
5758169689Skan	  else
5759169689Skan	    goto pop;
5760169689Skan	}
5761132718Skan
5762169689Skan     get_rhs:
5763169689Skan      tree_type = binops_by_token[token->type].tree_type;
5764132718Skan
5765169689Skan      /* We used the operator token.  */
5766169689Skan      cp_lexer_consume_token (parser->lexer);
5767132718Skan
5768169689Skan      /* Extract another operand.  It may be the RHS of this expression
5769169689Skan	 or the LHS of a new, higher priority expression.  */
5770169689Skan      rhs = cp_parser_simple_cast_expression (parser);
5771132718Skan
5772169689Skan      /* Get another operator token.  Look up its precedence to avoid
5773169689Skan	 building a useless (immediately popped) stack entry for common
5774169689Skan	 cases such as 3 + 4 + 5 or 3 * 4 + 5.  */
5775169689Skan      token = cp_lexer_peek_token (parser->lexer);
5776169689Skan      lookahead_prec = TOKEN_PRECEDENCE (token);
5777169689Skan      if (lookahead_prec > new_prec)
5778169689Skan	{
5779169689Skan	  /* ... and prepare to parse the RHS of the new, higher priority
5780169689Skan	     expression.  Since precedence levels on the stack are
5781169689Skan	     monotonically increasing, we do not have to care about
5782169689Skan	     stack overflows.  */
5783169689Skan	  sp->prec = prec;
5784169689Skan	  sp->tree_type = tree_type;
5785169689Skan	  sp->lhs = lhs;
5786169689Skan	  sp++;
5787169689Skan	  lhs = rhs;
5788169689Skan	  prec = new_prec;
5789169689Skan	  new_prec = lookahead_prec;
5790169689Skan	  goto get_rhs;
5791132718Skan
5792169689Skan	 pop:
5793169689Skan	  /* If the stack is not empty, we have parsed into LHS the right side
5794169689Skan	     (`4' in the example above) of an expression we had suspended.
5795169689Skan	     We can use the information on the stack to recover the LHS (`3')
5796169689Skan	     from the stack together with the tree code (`MULT_EXPR'), and
5797169689Skan	     the precedence of the higher level subexpression
5798169689Skan	     (`PREC_ADDITIVE_EXPRESSION').  TOKEN is the CPP_PLUS token,
5799169689Skan	     which will be used to actually build the additive expression.  */
5800169689Skan	  --sp;
5801169689Skan	  prec = sp->prec;
5802169689Skan	  tree_type = sp->tree_type;
5803169689Skan	  rhs = lhs;
5804169689Skan	  lhs = sp->lhs;
5805169689Skan	}
5806132718Skan
5807169689Skan      overloaded_p = false;
5808169689Skan      lhs = build_x_binary_op (tree_type, lhs, rhs, &overloaded_p);
5809132718Skan
5810169689Skan      /* If the binary operator required the use of an overloaded operator,
5811169689Skan	 then this expression cannot be an integral constant-expression.
5812169689Skan	 An overloaded operator can be used even if both operands are
5813169689Skan	 otherwise permissible in an integral constant-expression if at
5814169689Skan	 least one of the operands is of enumeration type.  */
5815169689Skan
5816169689Skan      if (overloaded_p
5817169689Skan	  && (cp_parser_non_integral_constant_expression
5818169689Skan	      (parser, "calls to overloaded operators")))
5819169689Skan	return error_mark_node;
5820169689Skan    }
5821169689Skan
5822169689Skan  return lhs;
5823132718Skan}
5824132718Skan
5825169689Skan
5826132718Skan/* Parse the `? expression : assignment-expression' part of a
5827132718Skan   conditional-expression.  The LOGICAL_OR_EXPR is the
5828132718Skan   logical-or-expression that started the conditional-expression.
5829132718Skan   Returns a representation of the entire conditional-expression.
5830132718Skan
5831132718Skan   This routine is used by cp_parser_assignment_expression.
5832132718Skan
5833132718Skan     ? expression : assignment-expression
5834169689Skan
5835132718Skan   GNU Extensions:
5836169689Skan
5837132718Skan     ? : assignment-expression */
5838132718Skan
5839132718Skanstatic tree
5840132718Skancp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
5841132718Skan{
5842132718Skan  tree expr;
5843132718Skan  tree assignment_expr;
5844132718Skan
5845132718Skan  /* Consume the `?' token.  */
5846132718Skan  cp_lexer_consume_token (parser->lexer);
5847132718Skan  if (cp_parser_allow_gnu_extensions_p (parser)
5848132718Skan      && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5849132718Skan    /* Implicit true clause.  */
5850132718Skan    expr = NULL_TREE;
5851132718Skan  else
5852132718Skan    /* Parse the expression.  */
5853169689Skan    expr = cp_parser_expression (parser, /*cast_p=*/false);
5854169689Skan
5855132718Skan  /* The next token should be a `:'.  */
5856132718Skan  cp_parser_require (parser, CPP_COLON, "`:'");
5857132718Skan  /* Parse the assignment-expression.  */
5858169689Skan  assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false);
5859132718Skan
5860132718Skan  /* Build the conditional-expression.  */
5861132718Skan  return build_x_conditional_expr (logical_or_expr,
5862132718Skan				   expr,
5863132718Skan				   assignment_expr);
5864132718Skan}
5865132718Skan
5866132718Skan/* Parse an assignment-expression.
5867132718Skan
5868132718Skan   assignment-expression:
5869132718Skan     conditional-expression
5870132718Skan     logical-or-expression assignment-operator assignment_expression
5871132718Skan     throw-expression
5872132718Skan
5873169689Skan   CAST_P is true if this expression is the target of a cast.
5874169689Skan
5875132718Skan   Returns a representation for the expression.  */
5876132718Skan
5877132718Skanstatic tree
5878169689Skancp_parser_assignment_expression (cp_parser* parser, bool cast_p)
5879132718Skan{
5880132718Skan  tree expr;
5881132718Skan
5882132718Skan  /* If the next token is the `throw' keyword, then we're looking at
5883132718Skan     a throw-expression.  */
5884132718Skan  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5885132718Skan    expr = cp_parser_throw_expression (parser);
5886132718Skan  /* Otherwise, it must be that we are looking at a
5887132718Skan     logical-or-expression.  */
5888132718Skan  else
5889132718Skan    {
5890169689Skan      /* Parse the binary expressions (logical-or-expression).  */
5891169689Skan      expr = cp_parser_binary_expression (parser, cast_p);
5892132718Skan      /* If the next token is a `?' then we're actually looking at a
5893132718Skan	 conditional-expression.  */
5894132718Skan      if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5895132718Skan	return cp_parser_question_colon_clause (parser, expr);
5896169689Skan      else
5897132718Skan	{
5898132718Skan	  enum tree_code assignment_operator;
5899132718Skan
5900132718Skan	  /* If it's an assignment-operator, we're using the second
5901132718Skan	     production.  */
5902169689Skan	  assignment_operator
5903132718Skan	    = cp_parser_assignment_operator_opt (parser);
5904132718Skan	  if (assignment_operator != ERROR_MARK)
5905132718Skan	    {
5906132718Skan	      tree rhs;
5907132718Skan
5908132718Skan	      /* Parse the right-hand side of the assignment.  */
5909169689Skan	      rhs = cp_parser_assignment_expression (parser, cast_p);
5910132718Skan	      /* An assignment may not appear in a
5911132718Skan		 constant-expression.  */
5912132718Skan	      if (cp_parser_non_integral_constant_expression (parser,
5913132718Skan							      "an assignment"))
5914132718Skan		return error_mark_node;
5915132718Skan	      /* Build the assignment expression.  */
5916169689Skan	      expr = build_x_modify_expr (expr,
5917169689Skan					  assignment_operator,
5918132718Skan					  rhs);
5919132718Skan	    }
5920132718Skan	}
5921132718Skan    }
5922132718Skan
5923132718Skan  return expr;
5924132718Skan}
5925132718Skan
5926132718Skan/* Parse an (optional) assignment-operator.
5927132718Skan
5928169689Skan   assignment-operator: one of
5929169689Skan     = *= /= %= += -= >>= <<= &= ^= |=
5930132718Skan
5931132718Skan   GNU Extension:
5932169689Skan
5933132718Skan   assignment-operator: one of
5934132718Skan     <?= >?=
5935132718Skan
5936132718Skan   If the next token is an assignment operator, the corresponding tree
5937132718Skan   code is returned, and the token is consumed.  For example, for
5938132718Skan   `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
5939132718Skan   NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
5940132718Skan   TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
5941132718Skan   operator, ERROR_MARK is returned.  */
5942132718Skan
5943132718Skanstatic enum tree_code
5944132718Skancp_parser_assignment_operator_opt (cp_parser* parser)
5945132718Skan{
5946132718Skan  enum tree_code op;
5947132718Skan  cp_token *token;
5948132718Skan
5949132718Skan  /* Peek at the next toen.  */
5950132718Skan  token = cp_lexer_peek_token (parser->lexer);
5951132718Skan
5952132718Skan  switch (token->type)
5953132718Skan    {
5954132718Skan    case CPP_EQ:
5955132718Skan      op = NOP_EXPR;
5956132718Skan      break;
5957132718Skan
5958132718Skan    case CPP_MULT_EQ:
5959132718Skan      op = MULT_EXPR;
5960132718Skan      break;
5961132718Skan
5962132718Skan    case CPP_DIV_EQ:
5963132718Skan      op = TRUNC_DIV_EXPR;
5964132718Skan      break;
5965132718Skan
5966132718Skan    case CPP_MOD_EQ:
5967132718Skan      op = TRUNC_MOD_EXPR;
5968132718Skan      break;
5969132718Skan
5970132718Skan    case CPP_PLUS_EQ:
5971132718Skan      op = PLUS_EXPR;
5972132718Skan      break;
5973132718Skan
5974132718Skan    case CPP_MINUS_EQ:
5975132718Skan      op = MINUS_EXPR;
5976132718Skan      break;
5977132718Skan
5978132718Skan    case CPP_RSHIFT_EQ:
5979132718Skan      op = RSHIFT_EXPR;
5980132718Skan      break;
5981132718Skan
5982132718Skan    case CPP_LSHIFT_EQ:
5983132718Skan      op = LSHIFT_EXPR;
5984132718Skan      break;
5985132718Skan
5986132718Skan    case CPP_AND_EQ:
5987132718Skan      op = BIT_AND_EXPR;
5988132718Skan      break;
5989132718Skan
5990132718Skan    case CPP_XOR_EQ:
5991132718Skan      op = BIT_XOR_EXPR;
5992132718Skan      break;
5993132718Skan
5994132718Skan    case CPP_OR_EQ:
5995132718Skan      op = BIT_IOR_EXPR;
5996132718Skan      break;
5997132718Skan
5998169689Skan    default:
5999132718Skan      /* Nothing else is an assignment operator.  */
6000132718Skan      op = ERROR_MARK;
6001132718Skan    }
6002132718Skan
6003132718Skan  /* If it was an assignment operator, consume it.  */
6004132718Skan  if (op != ERROR_MARK)
6005132718Skan    cp_lexer_consume_token (parser->lexer);
6006132718Skan
6007132718Skan  return op;
6008132718Skan}
6009132718Skan
6010132718Skan/* Parse an expression.
6011132718Skan
6012132718Skan   expression:
6013132718Skan     assignment-expression
6014132718Skan     expression , assignment-expression
6015132718Skan
6016169689Skan   CAST_P is true if this expression is the target of a cast.
6017169689Skan
6018132718Skan   Returns a representation of the expression.  */
6019132718Skan
6020132718Skanstatic tree
6021169689Skancp_parser_expression (cp_parser* parser, bool cast_p)
6022132718Skan{
6023132718Skan  tree expression = NULL_TREE;
6024132718Skan
6025132718Skan  while (true)
6026132718Skan    {
6027132718Skan      tree assignment_expression;
6028132718Skan
6029132718Skan      /* Parse the next assignment-expression.  */
6030169689Skan      assignment_expression
6031169689Skan	= cp_parser_assignment_expression (parser, cast_p);
6032132718Skan      /* If this is the first assignment-expression, we can just
6033132718Skan	 save it away.  */
6034132718Skan      if (!expression)
6035132718Skan	expression = assignment_expression;
6036132718Skan      else
6037132718Skan	expression = build_x_compound_expr (expression,
6038132718Skan					    assignment_expression);
6039132718Skan      /* If the next token is not a comma, then we are done with the
6040132718Skan	 expression.  */
6041132718Skan      if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6042132718Skan	break;
6043132718Skan      /* Consume the `,'.  */
6044132718Skan      cp_lexer_consume_token (parser->lexer);
6045132718Skan      /* A comma operator cannot appear in a constant-expression.  */
6046132718Skan      if (cp_parser_non_integral_constant_expression (parser,
6047132718Skan						      "a comma operator"))
6048132718Skan	expression = error_mark_node;
6049132718Skan    }
6050132718Skan
6051132718Skan  return expression;
6052132718Skan}
6053132718Skan
6054169689Skan/* Parse a constant-expression.
6055132718Skan
6056132718Skan   constant-expression:
6057169689Skan     conditional-expression
6058132718Skan
6059132718Skan  If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6060132718Skan  accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
6061132718Skan  constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
6062132718Skan  is false, NON_CONSTANT_P should be NULL.  */
6063132718Skan
6064132718Skanstatic tree
6065169689Skancp_parser_constant_expression (cp_parser* parser,
6066132718Skan			       bool allow_non_constant_p,
6067132718Skan			       bool *non_constant_p)
6068132718Skan{
6069132718Skan  bool saved_integral_constant_expression_p;
6070132718Skan  bool saved_allow_non_integral_constant_expression_p;
6071132718Skan  bool saved_non_integral_constant_expression_p;
6072132718Skan  tree expression;
6073132718Skan
6074132718Skan  /* It might seem that we could simply parse the
6075132718Skan     conditional-expression, and then check to see if it were
6076132718Skan     TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
6077132718Skan     one that the compiler can figure out is constant, possibly after
6078132718Skan     doing some simplifications or optimizations.  The standard has a
6079132718Skan     precise definition of constant-expression, and we must honor
6080132718Skan     that, even though it is somewhat more restrictive.
6081132718Skan
6082132718Skan     For example:
6083132718Skan
6084132718Skan       int i[(2, 3)];
6085132718Skan
6086132718Skan     is not a legal declaration, because `(2, 3)' is not a
6087132718Skan     constant-expression.  The `,' operator is forbidden in a
6088132718Skan     constant-expression.  However, GCC's constant-folding machinery
6089132718Skan     will fold this operation to an INTEGER_CST for `3'.  */
6090132718Skan
6091132718Skan  /* Save the old settings.  */
6092132718Skan  saved_integral_constant_expression_p = parser->integral_constant_expression_p;
6093169689Skan  saved_allow_non_integral_constant_expression_p
6094132718Skan    = parser->allow_non_integral_constant_expression_p;
6095132718Skan  saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
6096132718Skan  /* We are now parsing a constant-expression.  */
6097132718Skan  parser->integral_constant_expression_p = true;
6098132718Skan  parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
6099132718Skan  parser->non_integral_constant_expression_p = false;
6100132718Skan  /* Although the grammar says "conditional-expression", we parse an
6101132718Skan     "assignment-expression", which also permits "throw-expression"
6102132718Skan     and the use of assignment operators.  In the case that
6103132718Skan     ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6104132718Skan     otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
6105132718Skan     actually essential that we look for an assignment-expression.
6106132718Skan     For example, cp_parser_initializer_clauses uses this function to
6107132718Skan     determine whether a particular assignment-expression is in fact
6108132718Skan     constant.  */
6109169689Skan  expression = cp_parser_assignment_expression (parser, /*cast_p=*/false);
6110132718Skan  /* Restore the old settings.  */
6111169689Skan  parser->integral_constant_expression_p
6112169689Skan    = saved_integral_constant_expression_p;
6113169689Skan  parser->allow_non_integral_constant_expression_p
6114132718Skan    = saved_allow_non_integral_constant_expression_p;
6115132718Skan  if (allow_non_constant_p)
6116132718Skan    *non_constant_p = parser->non_integral_constant_expression_p;
6117169689Skan  else if (parser->non_integral_constant_expression_p)
6118169689Skan    expression = error_mark_node;
6119169689Skan  parser->non_integral_constant_expression_p
6120169689Skan    = saved_non_integral_constant_expression_p;
6121132718Skan
6122132718Skan  return expression;
6123132718Skan}
6124132718Skan
6125169689Skan/* Parse __builtin_offsetof.
6126169689Skan
6127169689Skan   offsetof-expression:
6128169689Skan     "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6129169689Skan
6130169689Skan   offsetof-member-designator:
6131169689Skan     id-expression
6132169689Skan     | offsetof-member-designator "." id-expression
6133169689Skan     | offsetof-member-designator "[" expression "]"  */
6134169689Skan
6135169689Skanstatic tree
6136169689Skancp_parser_builtin_offsetof (cp_parser *parser)
6137169689Skan{
6138169689Skan  int save_ice_p, save_non_ice_p;
6139169689Skan  tree type, expr;
6140169689Skan  cp_id_kind dummy;
6141169689Skan
6142169689Skan  /* We're about to accept non-integral-constant things, but will
6143169689Skan     definitely yield an integral constant expression.  Save and
6144169689Skan     restore these values around our local parsing.  */
6145169689Skan  save_ice_p = parser->integral_constant_expression_p;
6146169689Skan  save_non_ice_p = parser->non_integral_constant_expression_p;
6147169689Skan
6148169689Skan  /* Consume the "__builtin_offsetof" token.  */
6149169689Skan  cp_lexer_consume_token (parser->lexer);
6150169689Skan  /* Consume the opening `('.  */
6151169689Skan  cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6152169689Skan  /* Parse the type-id.  */
6153169689Skan  type = cp_parser_type_id (parser);
6154169689Skan  /* Look for the `,'.  */
6155169689Skan  cp_parser_require (parser, CPP_COMMA, "`,'");
6156169689Skan
6157169689Skan  /* Build the (type *)null that begins the traditional offsetof macro.  */
6158169689Skan  expr = build_static_cast (build_pointer_type (type), null_pointer_node);
6159169689Skan
6160169689Skan  /* Parse the offsetof-member-designator.  We begin as if we saw "expr->".  */
6161169689Skan  expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6162169689Skan						 true, &dummy);
6163169689Skan  while (true)
6164169689Skan    {
6165169689Skan      cp_token *token = cp_lexer_peek_token (parser->lexer);
6166169689Skan      switch (token->type)
6167169689Skan	{
6168169689Skan	case CPP_OPEN_SQUARE:
6169169689Skan	  /* offsetof-member-designator "[" expression "]" */
6170169689Skan	  expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6171169689Skan	  break;
6172169689Skan
6173169689Skan	case CPP_DOT:
6174169689Skan	  /* offsetof-member-designator "." identifier */
6175169689Skan	  cp_lexer_consume_token (parser->lexer);
6176169689Skan	  expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
6177169689Skan							 true, &dummy);
6178169689Skan	  break;
6179169689Skan
6180169689Skan	case CPP_CLOSE_PAREN:
6181169689Skan	  /* Consume the ")" token.  */
6182169689Skan	  cp_lexer_consume_token (parser->lexer);
6183169689Skan	  goto success;
6184169689Skan
6185169689Skan	default:
6186169689Skan	  /* Error.  We know the following require will fail, but
6187169689Skan	     that gives the proper error message.  */
6188169689Skan	  cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6189169689Skan	  cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6190169689Skan	  expr = error_mark_node;
6191169689Skan	  goto failure;
6192169689Skan	}
6193169689Skan    }
6194169689Skan
6195169689Skan success:
6196169689Skan  /* If we're processing a template, we can't finish the semantics yet.
6197169689Skan     Otherwise we can fold the entire expression now.  */
6198169689Skan  if (processing_template_decl)
6199169689Skan    expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6200169689Skan  else
6201169689Skan    expr = finish_offsetof (expr);
6202169689Skan
6203169689Skan failure:
6204169689Skan  parser->integral_constant_expression_p = save_ice_p;
6205169689Skan  parser->non_integral_constant_expression_p = save_non_ice_p;
6206169689Skan
6207169689Skan  return expr;
6208169689Skan}
6209169689Skan
6210132718Skan/* Statements [gram.stmt.stmt]  */
6211132718Skan
6212169689Skan/* Parse a statement.
6213132718Skan
6214132718Skan   statement:
6215132718Skan     labeled-statement
6216132718Skan     expression-statement
6217132718Skan     compound-statement
6218132718Skan     selection-statement
6219132718Skan     iteration-statement
6220132718Skan     jump-statement
6221132718Skan     declaration-statement
6222169689Skan     try-block
6223132718Skan
6224169689Skan  IN_COMPOUND is true when the statement is nested inside a
6225169689Skan  cp_parser_compound_statement; this matters for certain pragmas.  */
6226169689Skan
6227132718Skanstatic void
6228169689Skancp_parser_statement (cp_parser* parser, tree in_statement_expr,
6229169689Skan		     bool in_compound)
6230132718Skan{
6231132718Skan  tree statement;
6232132718Skan  cp_token *token;
6233169689Skan  location_t statement_location;
6234132718Skan
6235169689Skan restart:
6236132718Skan  /* There is no statement yet.  */
6237132718Skan  statement = NULL_TREE;
6238132718Skan  /* Peek at the next token.  */
6239132718Skan  token = cp_lexer_peek_token (parser->lexer);
6240169689Skan  /* Remember the location of the first token in the statement.  */
6241169689Skan  statement_location = token->location;
6242132718Skan  /* If this is a keyword, then that will often determine what kind of
6243132718Skan     statement we have.  */
6244132718Skan  if (token->type == CPP_KEYWORD)
6245132718Skan    {
6246132718Skan      enum rid keyword = token->keyword;
6247132718Skan
6248132718Skan      switch (keyword)
6249132718Skan	{
6250132718Skan	case RID_CASE:
6251132718Skan	case RID_DEFAULT:
6252169689Skan	  /* Looks like a labeled-statement with a case label.
6253169689Skan	     Parse the label, and then use tail recursion to parse
6254169689Skan	     the statement.  */
6255169689Skan	  cp_parser_label_for_labeled_statement (parser);
6256169689Skan	  goto restart;
6257132718Skan
6258132718Skan	case RID_IF:
6259132718Skan	case RID_SWITCH:
6260132718Skan	  statement = cp_parser_selection_statement (parser);
6261132718Skan	  break;
6262132718Skan
6263132718Skan	case RID_WHILE:
6264132718Skan	case RID_DO:
6265132718Skan	case RID_FOR:
6266132718Skan	  statement = cp_parser_iteration_statement (parser);
6267132718Skan	  break;
6268132718Skan
6269132718Skan	case RID_BREAK:
6270132718Skan	case RID_CONTINUE:
6271132718Skan	case RID_RETURN:
6272132718Skan	case RID_GOTO:
6273132718Skan	  statement = cp_parser_jump_statement (parser);
6274132718Skan	  break;
6275132718Skan
6276169689Skan	  /* Objective-C++ exception-handling constructs.  */
6277169689Skan	case RID_AT_TRY:
6278169689Skan	case RID_AT_CATCH:
6279169689Skan	case RID_AT_FINALLY:
6280169689Skan	case RID_AT_SYNCHRONIZED:
6281169689Skan	case RID_AT_THROW:
6282169689Skan	  statement = cp_parser_objc_statement (parser);
6283169689Skan	  break;
6284169689Skan
6285132718Skan	case RID_TRY:
6286132718Skan	  statement = cp_parser_try_block (parser);
6287132718Skan	  break;
6288132718Skan
6289132718Skan	default:
6290132718Skan	  /* It might be a keyword like `int' that can start a
6291132718Skan	     declaration-statement.  */
6292132718Skan	  break;
6293132718Skan	}
6294132718Skan    }
6295132718Skan  else if (token->type == CPP_NAME)
6296132718Skan    {
6297132718Skan      /* If the next token is a `:', then we are looking at a
6298132718Skan	 labeled-statement.  */
6299132718Skan      token = cp_lexer_peek_nth_token (parser->lexer, 2);
6300132718Skan      if (token->type == CPP_COLON)
6301169689Skan	{
6302169689Skan	  /* Looks like a labeled-statement with an ordinary label.
6303169689Skan	     Parse the label, and then use tail recursion to parse
6304169689Skan	     the statement.  */
6305169689Skan	  cp_parser_label_for_labeled_statement (parser);
6306169689Skan	  goto restart;
6307169689Skan	}
6308132718Skan    }
6309132718Skan  /* Anything that starts with a `{' must be a compound-statement.  */
6310132718Skan  else if (token->type == CPP_OPEN_BRACE)
6311169689Skan    statement = cp_parser_compound_statement (parser, NULL, false);
6312169689Skan  /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6313169689Skan     a statement all its own.  */
6314169689Skan  else if (token->type == CPP_PRAGMA)
6315169689Skan    {
6316169689Skan      /* Only certain OpenMP pragmas are attached to statements, and thus
6317169689Skan	 are considered statements themselves.  All others are not.  In
6318169689Skan	 the context of a compound, accept the pragma as a "statement" and
6319169689Skan	 return so that we can check for a close brace.  Otherwise we
6320169689Skan	 require a real statement and must go back and read one.  */
6321169689Skan      if (in_compound)
6322169689Skan	cp_parser_pragma (parser, pragma_compound);
6323169689Skan      else if (!cp_parser_pragma (parser, pragma_stmt))
6324169689Skan	goto restart;
6325169689Skan      return;
6326169689Skan    }
6327169689Skan  else if (token->type == CPP_EOF)
6328169689Skan    {
6329169689Skan      cp_parser_error (parser, "expected statement");
6330169689Skan      return;
6331169689Skan    }
6332132718Skan
6333132718Skan  /* Everything else must be a declaration-statement or an
6334169689Skan     expression-statement.  Try for the declaration-statement
6335132718Skan     first, unless we are looking at a `;', in which case we know that
6336132718Skan     we have an expression-statement.  */
6337132718Skan  if (!statement)
6338132718Skan    {
6339132718Skan      if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6340132718Skan	{
6341132718Skan	  cp_parser_parse_tentatively (parser);
6342132718Skan	  /* Try to parse the declaration-statement.  */
6343132718Skan	  cp_parser_declaration_statement (parser);
6344132718Skan	  /* If that worked, we're done.  */
6345132718Skan	  if (cp_parser_parse_definitely (parser))
6346132718Skan	    return;
6347132718Skan	}
6348132718Skan      /* Look for an expression-statement instead.  */
6349169689Skan      statement = cp_parser_expression_statement (parser, in_statement_expr);
6350132718Skan    }
6351132718Skan
6352132718Skan  /* Set the line number for the statement.  */
6353132718Skan  if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
6354169689Skan    SET_EXPR_LOCATION (statement, statement_location);
6355132718Skan}
6356132718Skan
6357169689Skan/* Parse the label for a labeled-statement, i.e.
6358132718Skan
6359169689Skan   identifier :
6360169689Skan   case constant-expression :
6361169689Skan   default :
6362132718Skan
6363132718Skan   GNU Extension:
6364169689Skan   case constant-expression ... constant-expression : statement
6365132718Skan
6366169689Skan   When a label is parsed without errors, the label is added to the
6367169689Skan   parse tree by the finish_* functions, so this function doesn't
6368169689Skan   have to return the label.  */
6369132718Skan
6370169689Skanstatic void
6371169689Skancp_parser_label_for_labeled_statement (cp_parser* parser)
6372132718Skan{
6373132718Skan  cp_token *token;
6374132718Skan
6375132718Skan  /* The next token should be an identifier.  */
6376132718Skan  token = cp_lexer_peek_token (parser->lexer);
6377132718Skan  if (token->type != CPP_NAME
6378132718Skan      && token->type != CPP_KEYWORD)
6379132718Skan    {
6380132718Skan      cp_parser_error (parser, "expected labeled-statement");
6381169689Skan      return;
6382132718Skan    }
6383132718Skan
6384132718Skan  switch (token->keyword)
6385132718Skan    {
6386132718Skan    case RID_CASE:
6387132718Skan      {
6388132718Skan	tree expr, expr_hi;
6389132718Skan	cp_token *ellipsis;
6390132718Skan
6391132718Skan	/* Consume the `case' token.  */
6392132718Skan	cp_lexer_consume_token (parser->lexer);
6393132718Skan	/* Parse the constant-expression.  */
6394169689Skan	expr = cp_parser_constant_expression (parser,
6395132718Skan					      /*allow_non_constant_p=*/false,
6396132718Skan					      NULL);
6397132718Skan
6398132718Skan	ellipsis = cp_lexer_peek_token (parser->lexer);
6399132718Skan	if (ellipsis->type == CPP_ELLIPSIS)
6400132718Skan	  {
6401169689Skan	    /* Consume the `...' token.  */
6402132718Skan	    cp_lexer_consume_token (parser->lexer);
6403132718Skan	    expr_hi =
6404132718Skan	      cp_parser_constant_expression (parser,
6405169689Skan					     /*allow_non_constant_p=*/false,
6406132718Skan					     NULL);
6407132718Skan	    /* We don't need to emit warnings here, as the common code
6408132718Skan	       will do this for us.  */
6409132718Skan	  }
6410132718Skan	else
6411132718Skan	  expr_hi = NULL_TREE;
6412132718Skan
6413169689Skan	if (parser->in_switch_statement_p)
6414169689Skan	  finish_case_label (expr, expr_hi);
6415132718Skan	else
6416169689Skan	  error ("case label %qE not within a switch statement", expr);
6417132718Skan      }
6418132718Skan      break;
6419132718Skan
6420132718Skan    case RID_DEFAULT:
6421132718Skan      /* Consume the `default' token.  */
6422132718Skan      cp_lexer_consume_token (parser->lexer);
6423169689Skan
6424169689Skan      if (parser->in_switch_statement_p)
6425169689Skan	finish_case_label (NULL_TREE, NULL_TREE);
6426169689Skan      else
6427132718Skan	error ("case label not within a switch statement");
6428132718Skan      break;
6429132718Skan
6430132718Skan    default:
6431132718Skan      /* Anything else must be an ordinary label.  */
6432169689Skan      finish_label_stmt (cp_parser_identifier (parser));
6433132718Skan      break;
6434132718Skan    }
6435132718Skan
6436132718Skan  /* Require the `:' token.  */
6437132718Skan  cp_parser_require (parser, CPP_COLON, "`:'");
6438132718Skan}
6439132718Skan
6440132718Skan/* Parse an expression-statement.
6441132718Skan
6442132718Skan   expression-statement:
6443132718Skan     expression [opt] ;
6444132718Skan
6445132718Skan   Returns the new EXPR_STMT -- or NULL_TREE if the expression
6446132718Skan   statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6447132718Skan   indicates whether this expression-statement is part of an
6448132718Skan   expression statement.  */
6449132718Skan
6450132718Skanstatic tree
6451169689Skancp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
6452132718Skan{
6453132718Skan  tree statement = NULL_TREE;
6454132718Skan
6455132718Skan  /* If the next token is a ';', then there is no expression
6456132718Skan     statement.  */
6457132718Skan  if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6458169689Skan    statement = cp_parser_expression (parser, /*cast_p=*/false);
6459169689Skan
6460132718Skan  /* Consume the final `;'.  */
6461132718Skan  cp_parser_consume_semicolon_at_end_of_statement (parser);
6462132718Skan
6463169689Skan  if (in_statement_expr
6464132718Skan      && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
6465169689Skan    /* This is the final expression statement of a statement
6466169689Skan       expression.  */
6467169689Skan    statement = finish_stmt_expr_expr (statement, in_statement_expr);
6468132718Skan  else if (statement)
6469132718Skan    statement = finish_expr_stmt (statement);
6470132718Skan  else
6471132718Skan    finish_stmt ();
6472169689Skan
6473132718Skan  return statement;
6474132718Skan}
6475132718Skan
6476132718Skan/* Parse a compound-statement.
6477132718Skan
6478132718Skan   compound-statement:
6479132718Skan     { statement-seq [opt] }
6480132718Skan
6481169689Skan   Returns a tree representing the statement.  */
6482169689Skan
6483132718Skanstatic tree
6484169689Skancp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
6485169689Skan			      bool in_try)
6486132718Skan{
6487132718Skan  tree compound_stmt;
6488132718Skan
6489132718Skan  /* Consume the `{'.  */
6490132718Skan  if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
6491132718Skan    return error_mark_node;
6492132718Skan  /* Begin the compound-statement.  */
6493169689Skan  compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
6494132718Skan  /* Parse an (optional) statement-seq.  */
6495169689Skan  cp_parser_statement_seq_opt (parser, in_statement_expr);
6496132718Skan  /* Finish the compound-statement.  */
6497132718Skan  finish_compound_stmt (compound_stmt);
6498132718Skan  /* Consume the `}'.  */
6499132718Skan  cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6500132718Skan
6501132718Skan  return compound_stmt;
6502132718Skan}
6503132718Skan
6504132718Skan/* Parse an (optional) statement-seq.
6505132718Skan
6506132718Skan   statement-seq:
6507132718Skan     statement
6508132718Skan     statement-seq [opt] statement  */
6509132718Skan
6510132718Skanstatic void
6511169689Skancp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
6512132718Skan{
6513132718Skan  /* Scan statements until there aren't any more.  */
6514132718Skan  while (true)
6515132718Skan    {
6516169689Skan      cp_token *token = cp_lexer_peek_token (parser->lexer);
6517169689Skan
6518132718Skan      /* If we're looking at a `}', then we've run out of statements.  */
6519169689Skan      if (token->type == CPP_CLOSE_BRACE
6520169689Skan	  || token->type == CPP_EOF
6521169689Skan	  || token->type == CPP_PRAGMA_EOL)
6522132718Skan	break;
6523132718Skan
6524132718Skan      /* Parse the statement.  */
6525169689Skan      cp_parser_statement (parser, in_statement_expr, true);
6526132718Skan    }
6527132718Skan}
6528132718Skan
6529132718Skan/* Parse a selection-statement.
6530132718Skan
6531132718Skan   selection-statement:
6532132718Skan     if ( condition ) statement
6533132718Skan     if ( condition ) statement else statement
6534169689Skan     switch ( condition ) statement
6535132718Skan
6536132718Skan   Returns the new IF_STMT or SWITCH_STMT.  */
6537132718Skan
6538132718Skanstatic tree
6539132718Skancp_parser_selection_statement (cp_parser* parser)
6540132718Skan{
6541132718Skan  cp_token *token;
6542132718Skan  enum rid keyword;
6543132718Skan
6544132718Skan  /* Peek at the next token.  */
6545132718Skan  token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
6546132718Skan
6547132718Skan  /* See what kind of keyword it is.  */
6548132718Skan  keyword = token->keyword;
6549132718Skan  switch (keyword)
6550132718Skan    {
6551132718Skan    case RID_IF:
6552132718Skan    case RID_SWITCH:
6553132718Skan      {
6554132718Skan	tree statement;
6555132718Skan	tree condition;
6556132718Skan
6557132718Skan	/* Look for the `('.  */
6558132718Skan	if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
6559132718Skan	  {
6560132718Skan	    cp_parser_skip_to_end_of_statement (parser);
6561132718Skan	    return error_mark_node;
6562132718Skan	  }
6563132718Skan
6564132718Skan	/* Begin the selection-statement.  */
6565132718Skan	if (keyword == RID_IF)
6566132718Skan	  statement = begin_if_stmt ();
6567132718Skan	else
6568132718Skan	  statement = begin_switch_stmt ();
6569132718Skan
6570132718Skan	/* Parse the condition.  */
6571132718Skan	condition = cp_parser_condition (parser);
6572132718Skan	/* Look for the `)'.  */
6573132718Skan	if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
6574132718Skan	  cp_parser_skip_to_closing_parenthesis (parser, true, false,
6575132718Skan						 /*consume_paren=*/true);
6576132718Skan
6577132718Skan	if (keyword == RID_IF)
6578132718Skan	  {
6579132718Skan	    /* Add the condition.  */
6580132718Skan	    finish_if_stmt_cond (condition, statement);
6581132718Skan
6582132718Skan	    /* Parse the then-clause.  */
6583169689Skan	    cp_parser_implicitly_scoped_statement (parser);
6584132718Skan	    finish_then_clause (statement);
6585132718Skan
6586132718Skan	    /* If the next token is `else', parse the else-clause.  */
6587132718Skan	    if (cp_lexer_next_token_is_keyword (parser->lexer,
6588132718Skan						RID_ELSE))
6589132718Skan	      {
6590132718Skan		/* Consume the `else' keyword.  */
6591132718Skan		cp_lexer_consume_token (parser->lexer);
6592169689Skan		begin_else_clause (statement);
6593132718Skan		/* Parse the else-clause.  */
6594169689Skan		cp_parser_implicitly_scoped_statement (parser);
6595132718Skan		finish_else_clause (statement);
6596132718Skan	      }
6597132718Skan
6598132718Skan	    /* Now we're all done with the if-statement.  */
6599169689Skan	    finish_if_stmt (statement);
6600132718Skan	  }
6601132718Skan	else
6602132718Skan	  {
6603132718Skan	    bool in_switch_statement_p;
6604169689Skan	    unsigned char in_statement;
6605132718Skan
6606132718Skan	    /* Add the condition.  */
6607132718Skan	    finish_switch_cond (condition, statement);
6608132718Skan
6609132718Skan	    /* Parse the body of the switch-statement.  */
6610132718Skan	    in_switch_statement_p = parser->in_switch_statement_p;
6611169689Skan	    in_statement = parser->in_statement;
6612132718Skan	    parser->in_switch_statement_p = true;
6613169689Skan	    parser->in_statement |= IN_SWITCH_STMT;
6614169689Skan	    cp_parser_implicitly_scoped_statement (parser);
6615132718Skan	    parser->in_switch_statement_p = in_switch_statement_p;
6616169689Skan	    parser->in_statement = in_statement;
6617132718Skan
6618132718Skan	    /* Now we're all done with the switch-statement.  */
6619132718Skan	    finish_switch_stmt (statement);
6620132718Skan	  }
6621132718Skan
6622132718Skan	return statement;
6623132718Skan      }
6624132718Skan      break;
6625132718Skan
6626132718Skan    default:
6627132718Skan      cp_parser_error (parser, "expected selection-statement");
6628132718Skan      return error_mark_node;
6629132718Skan    }
6630132718Skan}
6631132718Skan
6632169689Skan/* Parse a condition.
6633132718Skan
6634132718Skan   condition:
6635132718Skan     expression
6636169689Skan     type-specifier-seq declarator = assignment-expression
6637132718Skan
6638132718Skan   GNU Extension:
6639169689Skan
6640132718Skan   condition:
6641169689Skan     type-specifier-seq declarator asm-specification [opt]
6642132718Skan       attributes [opt] = assignment-expression
6643169689Skan
6644132718Skan   Returns the expression that should be tested.  */
6645132718Skan
6646132718Skanstatic tree
6647132718Skancp_parser_condition (cp_parser* parser)
6648132718Skan{
6649169689Skan  cp_decl_specifier_seq type_specifiers;
6650132718Skan  const char *saved_message;
6651132718Skan
6652132718Skan  /* Try the declaration first.  */
6653132718Skan  cp_parser_parse_tentatively (parser);
6654132718Skan  /* New types are not allowed in the type-specifier-seq for a
6655132718Skan     condition.  */
6656132718Skan  saved_message = parser->type_definition_forbidden_message;
6657132718Skan  parser->type_definition_forbidden_message
6658132718Skan    = "types may not be defined in conditions";
6659132718Skan  /* Parse the type-specifier-seq.  */
6660169689Skan  cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
6661169689Skan				&type_specifiers);
6662132718Skan  /* Restore the saved message.  */
6663132718Skan  parser->type_definition_forbidden_message = saved_message;
6664132718Skan  /* If all is well, we might be looking at a declaration.  */
6665132718Skan  if (!cp_parser_error_occurred (parser))
6666132718Skan    {
6667132718Skan      tree decl;
6668132718Skan      tree asm_specification;
6669132718Skan      tree attributes;
6670169689Skan      cp_declarator *declarator;
6671132718Skan      tree initializer = NULL_TREE;
6672169689Skan
6673132718Skan      /* Parse the declarator.  */
6674132718Skan      declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
6675132718Skan					 /*ctor_dtor_or_conv_p=*/NULL,
6676146895Skan					 /*parenthesized_p=*/NULL,
6677146895Skan					 /*member_p=*/false);
6678132718Skan      /* Parse the attributes.  */
6679132718Skan      attributes = cp_parser_attributes_opt (parser);
6680132718Skan      /* Parse the asm-specification.  */
6681132718Skan      asm_specification = cp_parser_asm_specification_opt (parser);
6682132718Skan      /* If the next token is not an `=', then we might still be
6683132718Skan	 looking at an expression.  For example:
6684169689Skan
6685132718Skan	   if (A(a).x)
6686169689Skan
6687132718Skan	 looks like a decl-specifier-seq and a declarator -- but then
6688132718Skan	 there is no `=', so this is an expression.  */
6689132718Skan      cp_parser_require (parser, CPP_EQ, "`='");
6690132718Skan      /* If we did see an `=', then we are looking at a declaration
6691132718Skan	 for sure.  */
6692132718Skan      if (cp_parser_parse_definitely (parser))
6693132718Skan	{
6694169689Skan	  tree pushed_scope;
6695169689Skan	  bool non_constant_p;
6696169689Skan
6697132718Skan	  /* Create the declaration.  */
6698169689Skan	  decl = start_decl (declarator, &type_specifiers,
6699132718Skan			     /*initialized_p=*/true,
6700169689Skan			     attributes, /*prefix_attributes=*/NULL_TREE,
6701169689Skan			     &pushed_scope);
6702132718Skan	  /* Parse the assignment-expression.  */
6703169689Skan	  initializer
6704169689Skan	    = cp_parser_constant_expression (parser,
6705169689Skan					     /*allow_non_constant_p=*/true,
6706169689Skan					     &non_constant_p);
6707169689Skan	  if (!non_constant_p)
6708169689Skan	    initializer = fold_non_dependent_expr (initializer);
6709169689Skan
6710132718Skan	  /* Process the initializer.  */
6711169689Skan	  cp_finish_decl (decl,
6712169689Skan			  initializer, !non_constant_p,
6713169689Skan			  asm_specification,
6714132718Skan			  LOOKUP_ONLYCONVERTING);
6715169689Skan
6716169689Skan	  if (pushed_scope)
6717169689Skan	    pop_scope (pushed_scope);
6718169689Skan
6719132718Skan	  return convert_from_reference (decl);
6720132718Skan	}
6721132718Skan    }
6722132718Skan  /* If we didn't even get past the declarator successfully, we are
6723132718Skan     definitely not looking at a declaration.  */
6724132718Skan  else
6725132718Skan    cp_parser_abort_tentative_parse (parser);
6726132718Skan
6727132718Skan  /* Otherwise, we are looking at an expression.  */
6728169689Skan  return cp_parser_expression (parser, /*cast_p=*/false);
6729132718Skan}
6730132718Skan
6731132718Skan/* Parse an iteration-statement.
6732132718Skan
6733132718Skan   iteration-statement:
6734132718Skan     while ( condition ) statement
6735132718Skan     do statement while ( expression ) ;
6736132718Skan     for ( for-init-statement condition [opt] ; expression [opt] )
6737132718Skan       statement
6738132718Skan
6739132718Skan   Returns the new WHILE_STMT, DO_STMT, or FOR_STMT.  */
6740132718Skan
6741132718Skanstatic tree
6742132718Skancp_parser_iteration_statement (cp_parser* parser)
6743132718Skan{
6744132718Skan  cp_token *token;
6745132718Skan  enum rid keyword;
6746132718Skan  tree statement;
6747169689Skan  unsigned char in_statement;
6748132718Skan
6749132718Skan  /* Peek at the next token.  */
6750132718Skan  token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
6751132718Skan  if (!token)
6752132718Skan    return error_mark_node;
6753132718Skan
6754132718Skan  /* Remember whether or not we are already within an iteration
6755169689Skan     statement.  */
6756169689Skan  in_statement = parser->in_statement;
6757132718Skan
6758132718Skan  /* See what kind of keyword it is.  */
6759132718Skan  keyword = token->keyword;
6760132718Skan  switch (keyword)
6761132718Skan    {
6762132718Skan    case RID_WHILE:
6763132718Skan      {
6764132718Skan	tree condition;
6765132718Skan
6766132718Skan	/* Begin the while-statement.  */
6767132718Skan	statement = begin_while_stmt ();
6768132718Skan	/* Look for the `('.  */
6769132718Skan	cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6770132718Skan	/* Parse the condition.  */
6771132718Skan	condition = cp_parser_condition (parser);
6772132718Skan	finish_while_stmt_cond (condition, statement);
6773132718Skan	/* Look for the `)'.  */
6774132718Skan	cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6775132718Skan	/* Parse the dependent statement.  */
6776169689Skan	parser->in_statement = IN_ITERATION_STMT;
6777132718Skan	cp_parser_already_scoped_statement (parser);
6778169689Skan	parser->in_statement = in_statement;
6779132718Skan	/* We're done with the while-statement.  */
6780132718Skan	finish_while_stmt (statement);
6781132718Skan      }
6782132718Skan      break;
6783132718Skan
6784132718Skan    case RID_DO:
6785132718Skan      {
6786132718Skan	tree expression;
6787132718Skan
6788132718Skan	/* Begin the do-statement.  */
6789132718Skan	statement = begin_do_stmt ();
6790132718Skan	/* Parse the body of the do-statement.  */
6791169689Skan	parser->in_statement = IN_ITERATION_STMT;
6792132718Skan	cp_parser_implicitly_scoped_statement (parser);
6793169689Skan	parser->in_statement = in_statement;
6794132718Skan	finish_do_body (statement);
6795132718Skan	/* Look for the `while' keyword.  */
6796132718Skan	cp_parser_require_keyword (parser, RID_WHILE, "`while'");
6797132718Skan	/* Look for the `('.  */
6798132718Skan	cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6799132718Skan	/* Parse the expression.  */
6800169689Skan	expression = cp_parser_expression (parser, /*cast_p=*/false);
6801132718Skan	/* We're done with the do-statement.  */
6802132718Skan	finish_do_stmt (expression, statement);
6803132718Skan	/* Look for the `)'.  */
6804132718Skan	cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6805132718Skan	/* Look for the `;'.  */
6806132718Skan	cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6807132718Skan      }
6808132718Skan      break;
6809132718Skan
6810132718Skan    case RID_FOR:
6811132718Skan      {
6812132718Skan	tree condition = NULL_TREE;
6813132718Skan	tree expression = NULL_TREE;
6814132718Skan
6815132718Skan	/* Begin the for-statement.  */
6816132718Skan	statement = begin_for_stmt ();
6817132718Skan	/* Look for the `('.  */
6818132718Skan	cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6819132718Skan	/* Parse the initialization.  */
6820132718Skan	cp_parser_for_init_statement (parser);
6821132718Skan	finish_for_init_stmt (statement);
6822132718Skan
6823132718Skan	/* If there's a condition, process it.  */
6824132718Skan	if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6825132718Skan	  condition = cp_parser_condition (parser);
6826132718Skan	finish_for_cond (condition, statement);
6827132718Skan	/* Look for the `;'.  */
6828132718Skan	cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6829132718Skan
6830132718Skan	/* If there's an expression, process it.  */
6831132718Skan	if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6832169689Skan	  expression = cp_parser_expression (parser, /*cast_p=*/false);
6833132718Skan	finish_for_expr (expression, statement);
6834132718Skan	/* Look for the `)'.  */
6835132718Skan	cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6836169689Skan
6837132718Skan	/* Parse the body of the for-statement.  */
6838169689Skan	parser->in_statement = IN_ITERATION_STMT;
6839132718Skan	cp_parser_already_scoped_statement (parser);
6840169689Skan	parser->in_statement = in_statement;
6841132718Skan
6842132718Skan	/* We're done with the for-statement.  */
6843132718Skan	finish_for_stmt (statement);
6844132718Skan      }
6845132718Skan      break;
6846132718Skan
6847132718Skan    default:
6848132718Skan      cp_parser_error (parser, "expected iteration-statement");
6849132718Skan      statement = error_mark_node;
6850132718Skan      break;
6851132718Skan    }
6852132718Skan
6853132718Skan  return statement;
6854132718Skan}
6855132718Skan
6856132718Skan/* Parse a for-init-statement.
6857132718Skan
6858132718Skan   for-init-statement:
6859132718Skan     expression-statement
6860132718Skan     simple-declaration  */
6861132718Skan
6862132718Skanstatic void
6863132718Skancp_parser_for_init_statement (cp_parser* parser)
6864132718Skan{
6865132718Skan  /* If the next token is a `;', then we have an empty
6866132718Skan     expression-statement.  Grammatically, this is also a
6867132718Skan     simple-declaration, but an invalid one, because it does not
6868132718Skan     declare anything.  Therefore, if we did not handle this case
6869132718Skan     specially, we would issue an error message about an invalid
6870132718Skan     declaration.  */
6871132718Skan  if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6872132718Skan    {
6873132718Skan      /* We're going to speculatively look for a declaration, falling back
6874132718Skan	 to an expression, if necessary.  */
6875132718Skan      cp_parser_parse_tentatively (parser);
6876132718Skan      /* Parse the declaration.  */
6877132718Skan      cp_parser_simple_declaration (parser,
6878132718Skan				    /*function_definition_allowed_p=*/false);
6879132718Skan      /* If the tentative parse failed, then we shall need to look for an
6880132718Skan	 expression-statement.  */
6881132718Skan      if (cp_parser_parse_definitely (parser))
6882132718Skan	return;
6883132718Skan    }
6884132718Skan
6885132718Skan  cp_parser_expression_statement (parser, false);
6886132718Skan}
6887132718Skan
6888132718Skan/* Parse a jump-statement.
6889132718Skan
6890132718Skan   jump-statement:
6891132718Skan     break ;
6892132718Skan     continue ;
6893132718Skan     return expression [opt] ;
6894169689Skan     goto identifier ;
6895132718Skan
6896132718Skan   GNU extension:
6897132718Skan
6898132718Skan   jump-statement:
6899132718Skan     goto * expression ;
6900132718Skan
6901169689Skan   Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
6902132718Skan
6903132718Skanstatic tree
6904132718Skancp_parser_jump_statement (cp_parser* parser)
6905132718Skan{
6906132718Skan  tree statement = error_mark_node;
6907132718Skan  cp_token *token;
6908132718Skan  enum rid keyword;
6909132718Skan
6910132718Skan  /* Peek at the next token.  */
6911132718Skan  token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6912132718Skan  if (!token)
6913132718Skan    return error_mark_node;
6914132718Skan
6915132718Skan  /* See what kind of keyword it is.  */
6916132718Skan  keyword = token->keyword;
6917132718Skan  switch (keyword)
6918132718Skan    {
6919132718Skan    case RID_BREAK:
6920169689Skan      switch (parser->in_statement)
6921132718Skan	{
6922169689Skan	case 0:
6923132718Skan	  error ("break statement not within loop or switch");
6924169689Skan	  break;
6925169689Skan	default:
6926169689Skan	  gcc_assert ((parser->in_statement & IN_SWITCH_STMT)
6927169689Skan		      || parser->in_statement == IN_ITERATION_STMT);
6928169689Skan	  statement = finish_break_stmt ();
6929169689Skan	  break;
6930169689Skan	case IN_OMP_BLOCK:
6931169689Skan	  error ("invalid exit from OpenMP structured block");
6932169689Skan	  break;
6933169689Skan	case IN_OMP_FOR:
6934169689Skan	  error ("break statement used with OpenMP for loop");
6935169689Skan	  break;
6936132718Skan	}
6937169689Skan      cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6938132718Skan      break;
6939132718Skan
6940132718Skan    case RID_CONTINUE:
6941169689Skan      switch (parser->in_statement & ~IN_SWITCH_STMT)
6942132718Skan	{
6943169689Skan	case 0:
6944132718Skan	  error ("continue statement not within a loop");
6945169689Skan	  break;
6946169689Skan	case IN_ITERATION_STMT:
6947169689Skan	case IN_OMP_FOR:
6948169689Skan	  statement = finish_continue_stmt ();
6949169689Skan	  break;
6950169689Skan	case IN_OMP_BLOCK:
6951169689Skan	  error ("invalid exit from OpenMP structured block");
6952169689Skan	  break;
6953169689Skan	default:
6954169689Skan	  gcc_unreachable ();
6955132718Skan	}
6956169689Skan      cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6957132718Skan      break;
6958132718Skan
6959132718Skan    case RID_RETURN:
6960132718Skan      {
6961132718Skan	tree expr;
6962132718Skan
6963169689Skan	/* If the next token is a `;', then there is no
6964132718Skan	   expression.  */
6965132718Skan	if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6966169689Skan	  expr = cp_parser_expression (parser, /*cast_p=*/false);
6967132718Skan	else
6968132718Skan	  expr = NULL_TREE;
6969132718Skan	/* Build the return-statement.  */
6970132718Skan	statement = finish_return_stmt (expr);
6971132718Skan	/* Look for the final `;'.  */
6972169689Skan	cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6973132718Skan      }
6974132718Skan      break;
6975132718Skan
6976132718Skan    case RID_GOTO:
6977132718Skan      /* Create the goto-statement.  */
6978132718Skan      if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6979132718Skan	{
6980132718Skan	  /* Issue a warning about this use of a GNU extension.  */
6981132718Skan	  if (pedantic)
6982132718Skan	    pedwarn ("ISO C++ forbids computed gotos");
6983132718Skan	  /* Consume the '*' token.  */
6984132718Skan	  cp_lexer_consume_token (parser->lexer);
6985132718Skan	  /* Parse the dependent expression.  */
6986169689Skan	  finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false));
6987132718Skan	}
6988132718Skan      else
6989132718Skan	finish_goto_stmt (cp_parser_identifier (parser));
6990132718Skan      /* Look for the final `;'.  */
6991169689Skan      cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6992132718Skan      break;
6993132718Skan
6994132718Skan    default:
6995132718Skan      cp_parser_error (parser, "expected jump-statement");
6996132718Skan      break;
6997132718Skan    }
6998132718Skan
6999132718Skan  return statement;
7000132718Skan}
7001132718Skan
7002132718Skan/* Parse a declaration-statement.
7003132718Skan
7004132718Skan   declaration-statement:
7005132718Skan     block-declaration  */
7006132718Skan
7007132718Skanstatic void
7008132718Skancp_parser_declaration_statement (cp_parser* parser)
7009132718Skan{
7010169689Skan  void *p;
7011169689Skan
7012169689Skan  /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
7013169689Skan  p = obstack_alloc (&declarator_obstack, 0);
7014169689Skan
7015169689Skan /* Parse the block-declaration.  */
7016132718Skan  cp_parser_block_declaration (parser, /*statement_p=*/true);
7017132718Skan
7018169689Skan  /* Free any declarators allocated.  */
7019169689Skan  obstack_free (&declarator_obstack, p);
7020169689Skan
7021132718Skan  /* Finish off the statement.  */
7022132718Skan  finish_stmt ();
7023132718Skan}
7024132718Skan
7025132718Skan/* Some dependent statements (like `if (cond) statement'), are
7026132718Skan   implicitly in their own scope.  In other words, if the statement is
7027132718Skan   a single statement (as opposed to a compound-statement), it is
7028132718Skan   none-the-less treated as if it were enclosed in braces.  Any
7029132718Skan   declarations appearing in the dependent statement are out of scope
7030132718Skan   after control passes that point.  This function parses a statement,
7031132718Skan   but ensures that is in its own scope, even if it is not a
7032169689Skan   compound-statement.
7033132718Skan
7034132718Skan   Returns the new statement.  */
7035132718Skan
7036132718Skanstatic tree
7037132718Skancp_parser_implicitly_scoped_statement (cp_parser* parser)
7038132718Skan{
7039132718Skan  tree statement;
7040132718Skan
7041169689Skan  /* Mark if () ; with a special NOP_EXPR.  */
7042169689Skan  if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7043169689Skan    {
7044169689Skan      cp_lexer_consume_token (parser->lexer);
7045169689Skan      statement = add_stmt (build_empty_stmt ());
7046169689Skan    }
7047169689Skan  /* if a compound is opened, we simply parse the statement directly.  */
7048169689Skan  else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7049169689Skan    statement = cp_parser_compound_statement (parser, NULL, false);
7050132718Skan  /* If the token is not a `{', then we must take special action.  */
7051169689Skan  else
7052132718Skan    {
7053132718Skan      /* Create a compound-statement.  */
7054169689Skan      statement = begin_compound_stmt (0);
7055132718Skan      /* Parse the dependent-statement.  */
7056169689Skan      cp_parser_statement (parser, NULL_TREE, false);
7057132718Skan      /* Finish the dummy compound-statement.  */
7058132718Skan      finish_compound_stmt (statement);
7059132718Skan    }
7060132718Skan
7061132718Skan  /* Return the statement.  */
7062132718Skan  return statement;
7063132718Skan}
7064132718Skan
7065132718Skan/* For some dependent statements (like `while (cond) statement'), we
7066132718Skan   have already created a scope.  Therefore, even if the dependent
7067132718Skan   statement is a compound-statement, we do not want to create another
7068132718Skan   scope.  */
7069132718Skan
7070132718Skanstatic void
7071132718Skancp_parser_already_scoped_statement (cp_parser* parser)
7072132718Skan{
7073169689Skan  /* If the token is a `{', then we must take special action.  */
7074169689Skan  if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7075169689Skan    cp_parser_statement (parser, NULL_TREE, false);
7076169689Skan  else
7077132718Skan    {
7078169689Skan      /* Avoid calling cp_parser_compound_statement, so that we
7079169689Skan	 don't create a new scope.  Do everything else by hand.  */
7080169689Skan      cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
7081169689Skan      cp_parser_statement_seq_opt (parser, NULL_TREE);
7082169689Skan      cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7083132718Skan    }
7084132718Skan}
7085132718Skan
7086132718Skan/* Declarations [gram.dcl.dcl] */
7087132718Skan
7088132718Skan/* Parse an optional declaration-sequence.
7089132718Skan
7090132718Skan   declaration-seq:
7091132718Skan     declaration
7092132718Skan     declaration-seq declaration  */
7093132718Skan
7094132718Skanstatic void
7095132718Skancp_parser_declaration_seq_opt (cp_parser* parser)
7096132718Skan{
7097132718Skan  while (true)
7098132718Skan    {
7099132718Skan      cp_token *token;
7100132718Skan
7101132718Skan      token = cp_lexer_peek_token (parser->lexer);
7102132718Skan
7103132718Skan      if (token->type == CPP_CLOSE_BRACE
7104169689Skan	  || token->type == CPP_EOF
7105169689Skan	  || token->type == CPP_PRAGMA_EOL)
7106132718Skan	break;
7107132718Skan
7108169689Skan      if (token->type == CPP_SEMICOLON)
7109132718Skan	{
7110132718Skan	  /* A declaration consisting of a single semicolon is
7111132718Skan	     invalid.  Allow it unless we're being pedantic.  */
7112169689Skan	  cp_lexer_consume_token (parser->lexer);
7113132718Skan	  if (pedantic && !in_system_header)
7114169689Skan	    pedwarn ("extra %<;%>");
7115132718Skan	  continue;
7116132718Skan	}
7117132718Skan
7118169689Skan      /* If we're entering or exiting a region that's implicitly
7119169689Skan	 extern "C", modify the lang context appropriately.  */
7120169689Skan      if (!parser->implicit_extern_c && token->implicit_extern_c)
7121132718Skan	{
7122132718Skan	  push_lang_context (lang_name_c);
7123169689Skan	  parser->implicit_extern_c = true;
7124132718Skan	}
7125169689Skan      else if (parser->implicit_extern_c && !token->implicit_extern_c)
7126132718Skan	{
7127132718Skan	  pop_lang_context ();
7128169689Skan	  parser->implicit_extern_c = false;
7129132718Skan	}
7130132718Skan
7131169689Skan      if (token->type == CPP_PRAGMA)
7132169689Skan	{
7133169689Skan	  /* A top-level declaration can consist solely of a #pragma.
7134169689Skan	     A nested declaration cannot, so this is done here and not
7135169689Skan	     in cp_parser_declaration.  (A #pragma at block scope is
7136169689Skan	     handled in cp_parser_statement.)  */
7137169689Skan	  cp_parser_pragma (parser, pragma_external);
7138169689Skan	  continue;
7139169689Skan	}
7140169689Skan
7141132718Skan      /* Parse the declaration itself.  */
7142132718Skan      cp_parser_declaration (parser);
7143132718Skan    }
7144132718Skan}
7145132718Skan
7146132718Skan/* Parse a declaration.
7147132718Skan
7148132718Skan   declaration:
7149132718Skan     block-declaration
7150132718Skan     function-definition
7151132718Skan     template-declaration
7152132718Skan     explicit-instantiation
7153132718Skan     explicit-specialization
7154132718Skan     linkage-specification
7155169689Skan     namespace-definition
7156132718Skan
7157132718Skan   GNU extension:
7158132718Skan
7159132718Skan   declaration:
7160132718Skan      __extension__ declaration */
7161132718Skan
7162132718Skanstatic void
7163132718Skancp_parser_declaration (cp_parser* parser)
7164132718Skan{
7165132718Skan  cp_token token1;
7166132718Skan  cp_token token2;
7167132718Skan  int saved_pedantic;
7168169689Skan  void *p;
7169132718Skan
7170132718Skan  /* Check for the `__extension__' keyword.  */
7171132718Skan  if (cp_parser_extension_opt (parser, &saved_pedantic))
7172132718Skan    {
7173132718Skan      /* Parse the qualified declaration.  */
7174132718Skan      cp_parser_declaration (parser);
7175132718Skan      /* Restore the PEDANTIC flag.  */
7176132718Skan      pedantic = saved_pedantic;
7177132718Skan
7178132718Skan      return;
7179132718Skan    }
7180132718Skan
7181132718Skan  /* Try to figure out what kind of declaration is present.  */
7182132718Skan  token1 = *cp_lexer_peek_token (parser->lexer);
7183169689Skan
7184132718Skan  if (token1.type != CPP_EOF)
7185132718Skan    token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
7186169689Skan  else
7187169689Skan    {
7188169689Skan      token2.type = CPP_EOF;
7189169689Skan      token2.keyword = RID_MAX;
7190169689Skan    }
7191132718Skan
7192169689Skan  /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
7193169689Skan  p = obstack_alloc (&declarator_obstack, 0);
7194169689Skan
7195132718Skan  /* If the next token is `extern' and the following token is a string
7196132718Skan     literal, then we have a linkage specification.  */
7197132718Skan  if (token1.keyword == RID_EXTERN
7198132718Skan      && cp_parser_is_string_literal (&token2))
7199132718Skan    cp_parser_linkage_specification (parser);
7200132718Skan  /* If the next token is `template', then we have either a template
7201132718Skan     declaration, an explicit instantiation, or an explicit
7202132718Skan     specialization.  */
7203132718Skan  else if (token1.keyword == RID_TEMPLATE)
7204132718Skan    {
7205132718Skan      /* `template <>' indicates a template specialization.  */
7206132718Skan      if (token2.type == CPP_LESS
7207132718Skan	  && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
7208132718Skan	cp_parser_explicit_specialization (parser);
7209132718Skan      /* `template <' indicates a template declaration.  */
7210132718Skan      else if (token2.type == CPP_LESS)
7211132718Skan	cp_parser_template_declaration (parser, /*member_p=*/false);
7212132718Skan      /* Anything else must be an explicit instantiation.  */
7213132718Skan      else
7214132718Skan	cp_parser_explicit_instantiation (parser);
7215132718Skan    }
7216132718Skan  /* If the next token is `export', then we have a template
7217132718Skan     declaration.  */
7218132718Skan  else if (token1.keyword == RID_EXPORT)
7219132718Skan    cp_parser_template_declaration (parser, /*member_p=*/false);
7220132718Skan  /* If the next token is `extern', 'static' or 'inline' and the one
7221132718Skan     after that is `template', we have a GNU extended explicit
7222132718Skan     instantiation directive.  */
7223132718Skan  else if (cp_parser_allow_gnu_extensions_p (parser)
7224132718Skan	   && (token1.keyword == RID_EXTERN
7225132718Skan	       || token1.keyword == RID_STATIC
7226132718Skan	       || token1.keyword == RID_INLINE)
7227132718Skan	   && token2.keyword == RID_TEMPLATE)
7228132718Skan    cp_parser_explicit_instantiation (parser);
7229132718Skan  /* If the next token is `namespace', check for a named or unnamed
7230132718Skan     namespace definition.  */
7231132718Skan  else if (token1.keyword == RID_NAMESPACE
7232132718Skan	   && (/* A named namespace definition.  */
7233132718Skan	       (token2.type == CPP_NAME
7234169689Skan		&& (cp_lexer_peek_nth_token (parser->lexer, 3)->type
7235169689Skan		    != CPP_EQ))
7236132718Skan	       /* An unnamed namespace definition.  */
7237169689Skan	       || token2.type == CPP_OPEN_BRACE
7238169689Skan	       || token2.keyword == RID_ATTRIBUTE))
7239132718Skan    cp_parser_namespace_definition (parser);
7240169689Skan  /* Objective-C++ declaration/definition.  */
7241169689Skan  else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
7242169689Skan    cp_parser_objc_declaration (parser);
7243132718Skan  /* We must have either a block declaration or a function
7244132718Skan     definition.  */
7245132718Skan  else
7246132718Skan    /* Try to parse a block-declaration, or a function-definition.  */
7247132718Skan    cp_parser_block_declaration (parser, /*statement_p=*/false);
7248169689Skan
7249169689Skan  /* Free any declarators allocated.  */
7250169689Skan  obstack_free (&declarator_obstack, p);
7251132718Skan}
7252132718Skan
7253169689Skan/* Parse a block-declaration.
7254132718Skan
7255132718Skan   block-declaration:
7256132718Skan     simple-declaration
7257132718Skan     asm-definition
7258132718Skan     namespace-alias-definition
7259132718Skan     using-declaration
7260169689Skan     using-directive
7261132718Skan
7262132718Skan   GNU Extension:
7263132718Skan
7264132718Skan   block-declaration:
7265169689Skan     __extension__ block-declaration
7266132718Skan     label-declaration
7267132718Skan
7268132718Skan   If STATEMENT_P is TRUE, then this block-declaration is occurring as
7269132718Skan   part of a declaration-statement.  */
7270132718Skan
7271132718Skanstatic void
7272169689Skancp_parser_block_declaration (cp_parser *parser,
7273132718Skan			     bool      statement_p)
7274132718Skan{
7275132718Skan  cp_token *token1;
7276132718Skan  int saved_pedantic;
7277132718Skan
7278132718Skan  /* Check for the `__extension__' keyword.  */
7279132718Skan  if (cp_parser_extension_opt (parser, &saved_pedantic))
7280132718Skan    {
7281132718Skan      /* Parse the qualified declaration.  */
7282132718Skan      cp_parser_block_declaration (parser, statement_p);
7283132718Skan      /* Restore the PEDANTIC flag.  */
7284132718Skan      pedantic = saved_pedantic;
7285132718Skan
7286132718Skan      return;
7287132718Skan    }
7288132718Skan
7289132718Skan  /* Peek at the next token to figure out which kind of declaration is
7290132718Skan     present.  */
7291132718Skan  token1 = cp_lexer_peek_token (parser->lexer);
7292132718Skan
7293132718Skan  /* If the next keyword is `asm', we have an asm-definition.  */
7294132718Skan  if (token1->keyword == RID_ASM)
7295132718Skan    {
7296132718Skan      if (statement_p)
7297132718Skan	cp_parser_commit_to_tentative_parse (parser);
7298132718Skan      cp_parser_asm_definition (parser);
7299132718Skan    }
7300132718Skan  /* If the next keyword is `namespace', we have a
7301132718Skan     namespace-alias-definition.  */
7302132718Skan  else if (token1->keyword == RID_NAMESPACE)
7303132718Skan    cp_parser_namespace_alias_definition (parser);
7304132718Skan  /* If the next keyword is `using', we have either a
7305132718Skan     using-declaration or a using-directive.  */
7306132718Skan  else if (token1->keyword == RID_USING)
7307132718Skan    {
7308132718Skan      cp_token *token2;
7309132718Skan
7310132718Skan      if (statement_p)
7311132718Skan	cp_parser_commit_to_tentative_parse (parser);
7312132718Skan      /* If the token after `using' is `namespace', then we have a
7313132718Skan	 using-directive.  */
7314132718Skan      token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
7315132718Skan      if (token2->keyword == RID_NAMESPACE)
7316132718Skan	cp_parser_using_directive (parser);
7317132718Skan      /* Otherwise, it's a using-declaration.  */
7318132718Skan      else
7319169689Skan	cp_parser_using_declaration (parser,
7320169689Skan				     /*access_declaration_p=*/false);
7321132718Skan    }
7322132718Skan  /* If the next keyword is `__label__' we have a label declaration.  */
7323132718Skan  else if (token1->keyword == RID_LABEL)
7324132718Skan    {
7325132718Skan      if (statement_p)
7326132718Skan	cp_parser_commit_to_tentative_parse (parser);
7327132718Skan      cp_parser_label_declaration (parser);
7328132718Skan    }
7329132718Skan  /* Anything else must be a simple-declaration.  */
7330132718Skan  else
7331132718Skan    cp_parser_simple_declaration (parser, !statement_p);
7332132718Skan}
7333132718Skan
7334132718Skan/* Parse a simple-declaration.
7335132718Skan
7336132718Skan   simple-declaration:
7337169689Skan     decl-specifier-seq [opt] init-declarator-list [opt] ;
7338132718Skan
7339132718Skan   init-declarator-list:
7340132718Skan     init-declarator
7341169689Skan     init-declarator-list , init-declarator
7342132718Skan
7343132718Skan   If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
7344132718Skan   function-definition as a simple-declaration.  */
7345132718Skan
7346132718Skanstatic void
7347169689Skancp_parser_simple_declaration (cp_parser* parser,
7348169689Skan			      bool function_definition_allowed_p)
7349132718Skan{
7350169689Skan  cp_decl_specifier_seq decl_specifiers;
7351132718Skan  int declares_class_or_enum;
7352132718Skan  bool saw_declarator;
7353132718Skan
7354132718Skan  /* Defer access checks until we know what is being declared; the
7355132718Skan     checks for names appearing in the decl-specifier-seq should be
7356132718Skan     done as if we were in the scope of the thing being declared.  */
7357132718Skan  push_deferring_access_checks (dk_deferred);
7358132718Skan
7359132718Skan  /* Parse the decl-specifier-seq.  We have to keep track of whether
7360132718Skan     or not the decl-specifier-seq declares a named class or
7361132718Skan     enumeration type, since that is the only case in which the
7362169689Skan     init-declarator-list is allowed to be empty.
7363132718Skan
7364132718Skan     [dcl.dcl]
7365132718Skan
7366132718Skan     In a simple-declaration, the optional init-declarator-list can be
7367132718Skan     omitted only when declaring a class or enumeration, that is when
7368132718Skan     the decl-specifier-seq contains either a class-specifier, an
7369132718Skan     elaborated-type-specifier, or an enum-specifier.  */
7370169689Skan  cp_parser_decl_specifier_seq (parser,
7371169689Skan				CP_PARSER_FLAGS_OPTIONAL,
7372169689Skan				&decl_specifiers,
7373169689Skan				&declares_class_or_enum);
7374132718Skan  /* We no longer need to defer access checks.  */
7375132718Skan  stop_deferring_access_checks ();
7376132718Skan
7377132718Skan  /* In a block scope, a valid declaration must always have a
7378132718Skan     decl-specifier-seq.  By not trying to parse declarators, we can
7379132718Skan     resolve the declaration/expression ambiguity more quickly.  */
7380169689Skan  if (!function_definition_allowed_p
7381169689Skan      && !decl_specifiers.any_specifiers_p)
7382132718Skan    {
7383132718Skan      cp_parser_error (parser, "expected declaration");
7384132718Skan      goto done;
7385132718Skan    }
7386132718Skan
7387132718Skan  /* If the next two tokens are both identifiers, the code is
7388132718Skan     erroneous. The usual cause of this situation is code like:
7389132718Skan
7390132718Skan       T t;
7391132718Skan
7392132718Skan     where "T" should name a type -- but does not.  */
7393169689Skan  if (!decl_specifiers.type
7394169689Skan      && cp_parser_parse_and_diagnose_invalid_type_name (parser))
7395132718Skan    {
7396132718Skan      /* If parsing tentatively, we should commit; we really are
7397132718Skan	 looking at a declaration.  */
7398132718Skan      cp_parser_commit_to_tentative_parse (parser);
7399132718Skan      /* Give up.  */
7400132718Skan      goto done;
7401132718Skan    }
7402169689Skan
7403161651Skan  /* If we have seen at least one decl-specifier, and the next token
7404161651Skan     is not a parenthesis, then we must be looking at a declaration.
7405161651Skan     (After "int (" we might be looking at a functional cast.)  */
7406169689Skan  if (decl_specifiers.any_specifiers_p
7407161651Skan      && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7408161651Skan    cp_parser_commit_to_tentative_parse (parser);
7409132718Skan
7410132718Skan  /* Keep going until we hit the `;' at the end of the simple
7411132718Skan     declaration.  */
7412132718Skan  saw_declarator = false;
7413169689Skan  while (cp_lexer_next_token_is_not (parser->lexer,
7414132718Skan				     CPP_SEMICOLON))
7415132718Skan    {
7416132718Skan      cp_token *token;
7417132718Skan      bool function_definition_p;
7418132718Skan      tree decl;
7419132718Skan
7420169689Skan      if (saw_declarator)
7421169689Skan	{
7422169689Skan	  /* If we are processing next declarator, coma is expected */
7423169689Skan	  token = cp_lexer_peek_token (parser->lexer);
7424169689Skan	  gcc_assert (token->type == CPP_COMMA);
7425169689Skan	  cp_lexer_consume_token (parser->lexer);
7426169689Skan	}
7427169689Skan      else
7428169689Skan	saw_declarator = true;
7429169689Skan
7430132718Skan      /* Parse the init-declarator.  */
7431169689Skan      decl = cp_parser_init_declarator (parser, &decl_specifiers,
7432169689Skan					/*checks=*/NULL,
7433132718Skan					function_definition_allowed_p,
7434132718Skan					/*member_p=*/false,
7435132718Skan					declares_class_or_enum,
7436132718Skan					&function_definition_p);
7437132718Skan      /* If an error occurred while parsing tentatively, exit quickly.
7438132718Skan	 (That usually happens when in the body of a function; each
7439132718Skan	 statement is treated as a declaration-statement until proven
7440132718Skan	 otherwise.)  */
7441132718Skan      if (cp_parser_error_occurred (parser))
7442132718Skan	goto done;
7443132718Skan      /* Handle function definitions specially.  */
7444132718Skan      if (function_definition_p)
7445132718Skan	{
7446132718Skan	  /* If the next token is a `,', then we are probably
7447132718Skan	     processing something like:
7448132718Skan
7449132718Skan	       void f() {}, *p;
7450132718Skan
7451132718Skan	     which is erroneous.  */
7452132718Skan	  if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
7453132718Skan	    error ("mixing declarations and function-definitions is forbidden");
7454132718Skan	  /* Otherwise, we're done with the list of declarators.  */
7455132718Skan	  else
7456132718Skan	    {
7457132718Skan	      pop_deferring_access_checks ();
7458132718Skan	      return;
7459132718Skan	    }
7460132718Skan	}
7461132718Skan      /* The next token should be either a `,' or a `;'.  */
7462132718Skan      token = cp_lexer_peek_token (parser->lexer);
7463132718Skan      /* If it's a `,', there are more declarators to come.  */
7464132718Skan      if (token->type == CPP_COMMA)
7465169689Skan	/* will be consumed next time around */;
7466132718Skan      /* If it's a `;', we are done.  */
7467132718Skan      else if (token->type == CPP_SEMICOLON)
7468132718Skan	break;
7469132718Skan      /* Anything else is an error.  */
7470132718Skan      else
7471132718Skan	{
7472161651Skan	  /* If we have already issued an error message we don't need
7473161651Skan	     to issue another one.  */
7474161651Skan	  if (decl != error_mark_node
7475169689Skan	      || cp_parser_uncommitted_to_tentative_parse_p (parser))
7476169689Skan	    cp_parser_error (parser, "expected %<,%> or %<;%>");
7477132718Skan	  /* Skip tokens until we reach the end of the statement.  */
7478132718Skan	  cp_parser_skip_to_end_of_statement (parser);
7479132718Skan	  /* If the next token is now a `;', consume it.  */
7480132718Skan	  if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7481132718Skan	    cp_lexer_consume_token (parser->lexer);
7482132718Skan	  goto done;
7483132718Skan	}
7484132718Skan      /* After the first time around, a function-definition is not
7485132718Skan	 allowed -- even if it was OK at first.  For example:
7486132718Skan
7487169689Skan	   int i, f() {}
7488132718Skan
7489169689Skan	 is not valid.  */
7490132718Skan      function_definition_allowed_p = false;
7491132718Skan    }
7492132718Skan
7493132718Skan  /* Issue an error message if no declarators are present, and the
7494132718Skan     decl-specifier-seq does not itself declare a class or
7495132718Skan     enumeration.  */
7496132718Skan  if (!saw_declarator)
7497132718Skan    {
7498132718Skan      if (cp_parser_declares_only_class_p (parser))
7499169689Skan	shadow_tag (&decl_specifiers);
7500132718Skan      /* Perform any deferred access checks.  */
7501132718Skan      perform_deferred_access_checks ();
7502132718Skan    }
7503132718Skan
7504132718Skan  /* Consume the `;'.  */
7505132718Skan  cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7506132718Skan
7507132718Skan done:
7508132718Skan  pop_deferring_access_checks ();
7509132718Skan}
7510132718Skan
7511132718Skan/* Parse a decl-specifier-seq.
7512132718Skan
7513132718Skan   decl-specifier-seq:
7514132718Skan     decl-specifier-seq [opt] decl-specifier
7515132718Skan
7516132718Skan   decl-specifier:
7517132718Skan     storage-class-specifier
7518132718Skan     type-specifier
7519132718Skan     function-specifier
7520132718Skan     friend
7521169689Skan     typedef
7522132718Skan
7523132718Skan   GNU Extension:
7524132718Skan
7525132718Skan   decl-specifier:
7526132718Skan     attributes
7527132718Skan
7528169689Skan   Set *DECL_SPECS to a representation of the decl-specifier-seq.
7529132718Skan
7530169689Skan   The parser flags FLAGS is used to control type-specifier parsing.
7531132718Skan
7532132718Skan   *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
7533132718Skan   flags:
7534132718Skan
7535132718Skan     1: one of the decl-specifiers is an elaborated-type-specifier
7536169689Skan	(i.e., a type declaration)
7537132718Skan     2: one of the decl-specifiers is an enum-specifier or a
7538169689Skan	class-specifier (i.e., a type definition)
7539132718Skan
7540132718Skan   */
7541132718Skan
7542169689Skanstatic void
7543169689Skancp_parser_decl_specifier_seq (cp_parser* parser,
7544169689Skan			      cp_parser_flags flags,
7545169689Skan			      cp_decl_specifier_seq *decl_specs,
7546132718Skan			      int* declares_class_or_enum)
7547132718Skan{
7548132718Skan  bool constructor_possible_p = !parser->in_declarator_p;
7549169689Skan
7550169689Skan  /* Clear DECL_SPECS.  */
7551169689Skan  clear_decl_specs (decl_specs);
7552169689Skan
7553132718Skan  /* Assume no class or enumeration type is declared.  */
7554132718Skan  *declares_class_or_enum = 0;
7555132718Skan
7556132718Skan  /* Keep reading specifiers until there are no more to read.  */
7557132718Skan  while (true)
7558132718Skan    {
7559132718Skan      bool constructor_p;
7560169689Skan      bool found_decl_spec;
7561132718Skan      cp_token *token;
7562132718Skan
7563132718Skan      /* Peek at the next token.  */
7564132718Skan      token = cp_lexer_peek_token (parser->lexer);
7565132718Skan      /* Handle attributes.  */
7566132718Skan      if (token->keyword == RID_ATTRIBUTE)
7567132718Skan	{
7568132718Skan	  /* Parse the attributes.  */
7569169689Skan	  decl_specs->attributes
7570169689Skan	    = chainon (decl_specs->attributes,
7571169689Skan		       cp_parser_attributes_opt (parser));
7572132718Skan	  continue;
7573132718Skan	}
7574169689Skan      /* Assume we will find a decl-specifier keyword.  */
7575169689Skan      found_decl_spec = true;
7576132718Skan      /* If the next token is an appropriate keyword, we can simply
7577132718Skan	 add it to the list.  */
7578132718Skan      switch (token->keyword)
7579132718Skan	{
7580132718Skan	  /* decl-specifier:
7581132718Skan	       friend  */
7582169689Skan	case RID_FRIEND:
7583169689Skan	  if (!at_class_scope_p ())
7584169689Skan	    {
7585169689Skan	      error ("%<friend%> used outside of class");
7586169689Skan	      cp_lexer_purge_token (parser->lexer);
7587169689Skan	    }
7588132718Skan	  else
7589169689Skan	    {
7590169689Skan	      ++decl_specs->specs[(int) ds_friend];
7591169689Skan	      /* Consume the token.  */
7592169689Skan	      cp_lexer_consume_token (parser->lexer);
7593169689Skan	    }
7594132718Skan	  break;
7595132718Skan
7596132718Skan	  /* function-specifier:
7597132718Skan	       inline
7598132718Skan	       virtual
7599132718Skan	       explicit  */
7600132718Skan	case RID_INLINE:
7601132718Skan	case RID_VIRTUAL:
7602132718Skan	case RID_EXPLICIT:
7603169689Skan	  cp_parser_function_specifier_opt (parser, decl_specs);
7604132718Skan	  break;
7605169689Skan
7606132718Skan	  /* decl-specifier:
7607132718Skan	       typedef  */
7608132718Skan	case RID_TYPEDEF:
7609169689Skan	  ++decl_specs->specs[(int) ds_typedef];
7610132718Skan	  /* Consume the token.  */
7611132718Skan	  cp_lexer_consume_token (parser->lexer);
7612132718Skan	  /* A constructor declarator cannot appear in a typedef.  */
7613132718Skan	  constructor_possible_p = false;
7614132718Skan	  /* The "typedef" keyword can only occur in a declaration; we
7615132718Skan	     may as well commit at this point.  */
7616132718Skan	  cp_parser_commit_to_tentative_parse (parser);
7617169689Skan
7618169689Skan          if (decl_specs->storage_class != sc_none)
7619169689Skan            decl_specs->conflicting_specifiers_p = true;
7620132718Skan	  break;
7621132718Skan
7622132718Skan	  /* storage-class-specifier:
7623132718Skan	       auto
7624132718Skan	       register
7625132718Skan	       static
7626132718Skan	       extern
7627169689Skan	       mutable
7628132718Skan
7629169689Skan	     GNU Extension:
7630132718Skan	       thread  */
7631132718Skan	case RID_AUTO:
7632132718Skan	case RID_REGISTER:
7633132718Skan	case RID_STATIC:
7634132718Skan	case RID_EXTERN:
7635132718Skan	case RID_MUTABLE:
7636169689Skan	  /* Consume the token.  */
7637169689Skan	  cp_lexer_consume_token (parser->lexer);
7638169689Skan	  cp_parser_set_storage_class (parser, decl_specs, token->keyword);
7639169689Skan	  break;
7640132718Skan	case RID_THREAD:
7641169689Skan	  /* Consume the token.  */
7642169689Skan	  cp_lexer_consume_token (parser->lexer);
7643169689Skan	  ++decl_specs->specs[(int) ds_thread];
7644132718Skan	  break;
7645169689Skan
7646132718Skan	default:
7647169689Skan	  /* We did not yet find a decl-specifier yet.  */
7648169689Skan	  found_decl_spec = false;
7649132718Skan	  break;
7650132718Skan	}
7651132718Skan
7652132718Skan      /* Constructors are a special case.  The `S' in `S()' is not a
7653132718Skan	 decl-specifier; it is the beginning of the declarator.  */
7654169689Skan      constructor_p
7655169689Skan	= (!found_decl_spec
7656169689Skan	   && constructor_possible_p
7657169689Skan	   && (cp_parser_constructor_declarator_p
7658169689Skan	       (parser, decl_specs->specs[(int) ds_friend] != 0)));
7659132718Skan
7660132718Skan      /* If we don't have a DECL_SPEC yet, then we must be looking at
7661132718Skan	 a type-specifier.  */
7662169689Skan      if (!found_decl_spec && !constructor_p)
7663132718Skan	{
7664132718Skan	  int decl_spec_declares_class_or_enum;
7665132718Skan	  bool is_cv_qualifier;
7666169689Skan	  tree type_spec;
7667132718Skan
7668169689Skan	  type_spec
7669132718Skan	    = cp_parser_type_specifier (parser, flags,
7670169689Skan					decl_specs,
7671132718Skan					/*is_declaration=*/true,
7672132718Skan					&decl_spec_declares_class_or_enum,
7673132718Skan					&is_cv_qualifier);
7674132718Skan
7675132718Skan	  *declares_class_or_enum |= decl_spec_declares_class_or_enum;
7676132718Skan
7677132718Skan	  /* If this type-specifier referenced a user-defined type
7678132718Skan	     (a typedef, class-name, etc.), then we can't allow any
7679132718Skan	     more such type-specifiers henceforth.
7680132718Skan
7681132718Skan	     [dcl.spec]
7682132718Skan
7683132718Skan	     The longest sequence of decl-specifiers that could
7684132718Skan	     possibly be a type name is taken as the
7685132718Skan	     decl-specifier-seq of a declaration.  The sequence shall
7686132718Skan	     be self-consistent as described below.
7687132718Skan
7688132718Skan	     [dcl.type]
7689132718Skan
7690132718Skan	     As a general rule, at most one type-specifier is allowed
7691132718Skan	     in the complete decl-specifier-seq of a declaration.  The
7692132718Skan	     only exceptions are the following:
7693132718Skan
7694132718Skan	     -- const or volatile can be combined with any other
7695169689Skan		type-specifier.
7696132718Skan
7697132718Skan	     -- signed or unsigned can be combined with char, long,
7698132718Skan		short, or int.
7699132718Skan
7700132718Skan	     -- ..
7701132718Skan
7702132718Skan	     Example:
7703132718Skan
7704132718Skan	       typedef char* Pc;
7705132718Skan	       void g (const int Pc);
7706132718Skan
7707132718Skan	     Here, Pc is *not* part of the decl-specifier seq; it's
7708132718Skan	     the declarator.  Therefore, once we see a type-specifier
7709132718Skan	     (other than a cv-qualifier), we forbid any additional
7710132718Skan	     user-defined types.  We *do* still allow things like `int
7711132718Skan	     int' to be considered a decl-specifier-seq, and issue the
7712132718Skan	     error message later.  */
7713169689Skan	  if (type_spec && !is_cv_qualifier)
7714132718Skan	    flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
7715132718Skan	  /* A constructor declarator cannot follow a type-specifier.  */
7716169689Skan	  if (type_spec)
7717169689Skan	    {
7718169689Skan	      constructor_possible_p = false;
7719169689Skan	      found_decl_spec = true;
7720169689Skan	    }
7721132718Skan	}
7722132718Skan
7723132718Skan      /* If we still do not have a DECL_SPEC, then there are no more
7724132718Skan	 decl-specifiers.  */
7725169689Skan      if (!found_decl_spec)
7726169689Skan	break;
7727132718Skan
7728169689Skan      decl_specs->any_specifiers_p = true;
7729132718Skan      /* After we see one decl-specifier, further decl-specifiers are
7730132718Skan	 always optional.  */
7731132718Skan      flags |= CP_PARSER_FLAGS_OPTIONAL;
7732132718Skan    }
7733132718Skan
7734169689Skan  cp_parser_check_decl_spec (decl_specs);
7735169689Skan
7736132718Skan  /* Don't allow a friend specifier with a class definition.  */
7737169689Skan  if (decl_specs->specs[(int) ds_friend] != 0
7738169689Skan      && (*declares_class_or_enum & 2))
7739132718Skan    error ("class definition may not be declared a friend");
7740132718Skan}
7741132718Skan
7742169689Skan/* Parse an (optional) storage-class-specifier.
7743132718Skan
7744132718Skan   storage-class-specifier:
7745132718Skan     auto
7746132718Skan     register
7747132718Skan     static
7748132718Skan     extern
7749169689Skan     mutable
7750132718Skan
7751132718Skan   GNU Extension:
7752132718Skan
7753132718Skan   storage-class-specifier:
7754132718Skan     thread
7755132718Skan
7756132718Skan   Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
7757169689Skan
7758132718Skanstatic tree
7759132718Skancp_parser_storage_class_specifier_opt (cp_parser* parser)
7760132718Skan{
7761132718Skan  switch (cp_lexer_peek_token (parser->lexer)->keyword)
7762132718Skan    {
7763132718Skan    case RID_AUTO:
7764132718Skan    case RID_REGISTER:
7765132718Skan    case RID_STATIC:
7766132718Skan    case RID_EXTERN:
7767132718Skan    case RID_MUTABLE:
7768132718Skan    case RID_THREAD:
7769132718Skan      /* Consume the token.  */
7770169689Skan      return cp_lexer_consume_token (parser->lexer)->u.value;
7771132718Skan
7772132718Skan    default:
7773132718Skan      return NULL_TREE;
7774132718Skan    }
7775132718Skan}
7776132718Skan
7777169689Skan/* Parse an (optional) function-specifier.
7778132718Skan
7779132718Skan   function-specifier:
7780132718Skan     inline
7781132718Skan     virtual
7782132718Skan     explicit
7783132718Skan
7784169689Skan   Returns an IDENTIFIER_NODE corresponding to the keyword used.
7785169689Skan   Updates DECL_SPECS, if it is non-NULL.  */
7786169689Skan
7787132718Skanstatic tree
7788169689Skancp_parser_function_specifier_opt (cp_parser* parser,
7789169689Skan				  cp_decl_specifier_seq *decl_specs)
7790132718Skan{
7791132718Skan  switch (cp_lexer_peek_token (parser->lexer)->keyword)
7792132718Skan    {
7793132718Skan    case RID_INLINE:
7794169689Skan      if (decl_specs)
7795169689Skan	++decl_specs->specs[(int) ds_inline];
7796169689Skan      break;
7797169689Skan
7798132718Skan    case RID_VIRTUAL:
7799169689Skan      /* 14.5.2.3 [temp.mem]
7800169689Skan
7801169689Skan	 A member function template shall not be virtual.  */
7802169689Skan      if (PROCESSING_REAL_TEMPLATE_DECL_P ())
7803169689Skan	error ("templates may not be %<virtual%>");
7804169689Skan      else if (decl_specs)
7805169689Skan	++decl_specs->specs[(int) ds_virtual];
7806169689Skan      break;
7807169689Skan
7808132718Skan    case RID_EXPLICIT:
7809169689Skan      if (decl_specs)
7810169689Skan	++decl_specs->specs[(int) ds_explicit];
7811169689Skan      break;
7812132718Skan
7813132718Skan    default:
7814132718Skan      return NULL_TREE;
7815132718Skan    }
7816169689Skan
7817169689Skan  /* Consume the token.  */
7818169689Skan  return cp_lexer_consume_token (parser->lexer)->u.value;
7819132718Skan}
7820132718Skan
7821132718Skan/* Parse a linkage-specification.
7822132718Skan
7823132718Skan   linkage-specification:
7824132718Skan     extern string-literal { declaration-seq [opt] }
7825132718Skan     extern string-literal declaration  */
7826132718Skan
7827132718Skanstatic void
7828132718Skancp_parser_linkage_specification (cp_parser* parser)
7829132718Skan{
7830132718Skan  tree linkage;
7831132718Skan
7832132718Skan  /* Look for the `extern' keyword.  */
7833132718Skan  cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
7834132718Skan
7835169689Skan  /* Look for the string-literal.  */
7836169689Skan  linkage = cp_parser_string_literal (parser, false, false);
7837132718Skan
7838132718Skan  /* Transform the literal into an identifier.  If the literal is a
7839132718Skan     wide-character string, or contains embedded NULs, then we can't
7840132718Skan     handle it as the user wants.  */
7841169689Skan  if (strlen (TREE_STRING_POINTER (linkage))
7842169689Skan      != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
7843132718Skan    {
7844132718Skan      cp_parser_error (parser, "invalid linkage-specification");
7845132718Skan      /* Assume C++ linkage.  */
7846169689Skan      linkage = lang_name_cplusplus;
7847132718Skan    }
7848132718Skan  else
7849169689Skan    linkage = get_identifier (TREE_STRING_POINTER (linkage));
7850132718Skan
7851132718Skan  /* We're now using the new linkage.  */
7852132718Skan  push_lang_context (linkage);
7853132718Skan
7854132718Skan  /* If the next token is a `{', then we're using the first
7855132718Skan     production.  */
7856132718Skan  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7857132718Skan    {
7858132718Skan      /* Consume the `{' token.  */
7859132718Skan      cp_lexer_consume_token (parser->lexer);
7860132718Skan      /* Parse the declarations.  */
7861132718Skan      cp_parser_declaration_seq_opt (parser);
7862132718Skan      /* Look for the closing `}'.  */
7863132718Skan      cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7864132718Skan    }
7865132718Skan  /* Otherwise, there's just one declaration.  */
7866132718Skan  else
7867132718Skan    {
7868132718Skan      bool saved_in_unbraced_linkage_specification_p;
7869132718Skan
7870169689Skan      saved_in_unbraced_linkage_specification_p
7871132718Skan	= parser->in_unbraced_linkage_specification_p;
7872132718Skan      parser->in_unbraced_linkage_specification_p = true;
7873132718Skan      cp_parser_declaration (parser);
7874169689Skan      parser->in_unbraced_linkage_specification_p
7875132718Skan	= saved_in_unbraced_linkage_specification_p;
7876132718Skan    }
7877132718Skan
7878132718Skan  /* We're done with the linkage-specification.  */
7879132718Skan  pop_lang_context ();
7880132718Skan}
7881132718Skan
7882132718Skan/* Special member functions [gram.special] */
7883132718Skan
7884132718Skan/* Parse a conversion-function-id.
7885132718Skan
7886132718Skan   conversion-function-id:
7887169689Skan     operator conversion-type-id
7888132718Skan
7889132718Skan   Returns an IDENTIFIER_NODE representing the operator.  */
7890132718Skan
7891169689Skanstatic tree
7892132718Skancp_parser_conversion_function_id (cp_parser* parser)
7893132718Skan{
7894132718Skan  tree type;
7895132718Skan  tree saved_scope;
7896132718Skan  tree saved_qualifying_scope;
7897132718Skan  tree saved_object_scope;
7898169689Skan  tree pushed_scope = NULL_TREE;
7899132718Skan
7900132718Skan  /* Look for the `operator' token.  */
7901132718Skan  if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7902132718Skan    return error_mark_node;
7903132718Skan  /* When we parse the conversion-type-id, the current scope will be
7904132718Skan     reset.  However, we need that information in able to look up the
7905132718Skan     conversion function later, so we save it here.  */
7906132718Skan  saved_scope = parser->scope;
7907132718Skan  saved_qualifying_scope = parser->qualifying_scope;
7908132718Skan  saved_object_scope = parser->object_scope;
7909132718Skan  /* We must enter the scope of the class so that the names of
7910132718Skan     entities declared within the class are available in the
7911132718Skan     conversion-type-id.  For example, consider:
7912132718Skan
7913169689Skan       struct S {
7914169689Skan	 typedef int I;
7915132718Skan	 operator I();
7916132718Skan       };
7917132718Skan
7918132718Skan       S::operator I() { ... }
7919132718Skan
7920132718Skan     In order to see that `I' is a type-name in the definition, we
7921132718Skan     must be in the scope of `S'.  */
7922132718Skan  if (saved_scope)
7923169689Skan    pushed_scope = push_scope (saved_scope);
7924132718Skan  /* Parse the conversion-type-id.  */
7925132718Skan  type = cp_parser_conversion_type_id (parser);
7926132718Skan  /* Leave the scope of the class, if any.  */
7927169689Skan  if (pushed_scope)
7928169689Skan    pop_scope (pushed_scope);
7929132718Skan  /* Restore the saved scope.  */
7930132718Skan  parser->scope = saved_scope;
7931132718Skan  parser->qualifying_scope = saved_qualifying_scope;
7932132718Skan  parser->object_scope = saved_object_scope;
7933132718Skan  /* If the TYPE is invalid, indicate failure.  */
7934132718Skan  if (type == error_mark_node)
7935132718Skan    return error_mark_node;
7936132718Skan  return mangle_conv_op_name_for_type (type);
7937132718Skan}
7938132718Skan
7939132718Skan/* Parse a conversion-type-id:
7940132718Skan
7941132718Skan   conversion-type-id:
7942132718Skan     type-specifier-seq conversion-declarator [opt]
7943132718Skan
7944132718Skan   Returns the TYPE specified.  */
7945132718Skan
7946132718Skanstatic tree
7947132718Skancp_parser_conversion_type_id (cp_parser* parser)
7948132718Skan{
7949132718Skan  tree attributes;
7950169689Skan  cp_decl_specifier_seq type_specifiers;
7951169689Skan  cp_declarator *declarator;
7952169689Skan  tree type_specified;
7953132718Skan
7954132718Skan  /* Parse the attributes.  */
7955132718Skan  attributes = cp_parser_attributes_opt (parser);
7956132718Skan  /* Parse the type-specifiers.  */
7957169689Skan  cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
7958169689Skan				&type_specifiers);
7959132718Skan  /* If that didn't work, stop.  */
7960169689Skan  if (type_specifiers.type == error_mark_node)
7961132718Skan    return error_mark_node;
7962132718Skan  /* Parse the conversion-declarator.  */
7963132718Skan  declarator = cp_parser_conversion_declarator_opt (parser);
7964132718Skan
7965169689Skan  type_specified =  grokdeclarator (declarator, &type_specifiers, TYPENAME,
7966169689Skan				    /*initialized=*/0, &attributes);
7967169689Skan  if (attributes)
7968169689Skan    cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
7969169689Skan  return type_specified;
7970132718Skan}
7971132718Skan
7972132718Skan/* Parse an (optional) conversion-declarator.
7973132718Skan
7974132718Skan   conversion-declarator:
7975169689Skan     ptr-operator conversion-declarator [opt]
7976132718Skan
7977169689Skan   */
7978132718Skan
7979169689Skanstatic cp_declarator *
7980132718Skancp_parser_conversion_declarator_opt (cp_parser* parser)
7981132718Skan{
7982132718Skan  enum tree_code code;
7983132718Skan  tree class_type;
7984169689Skan  cp_cv_quals cv_quals;
7985132718Skan
7986132718Skan  /* We don't know if there's a ptr-operator next, or not.  */
7987132718Skan  cp_parser_parse_tentatively (parser);
7988132718Skan  /* Try the ptr-operator.  */
7989169689Skan  code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
7990132718Skan  /* If it worked, look for more conversion-declarators.  */
7991132718Skan  if (cp_parser_parse_definitely (parser))
7992132718Skan    {
7993169689Skan      cp_declarator *declarator;
7994132718Skan
7995169689Skan      /* Parse another optional declarator.  */
7996169689Skan      declarator = cp_parser_conversion_declarator_opt (parser);
7997132718Skan
7998169689Skan      /* Create the representation of the declarator.  */
7999169689Skan      if (class_type)
8000169689Skan	declarator = make_ptrmem_declarator (cv_quals, class_type,
8001132718Skan					     declarator);
8002169689Skan      else if (code == INDIRECT_REF)
8003169689Skan	declarator = make_pointer_declarator (cv_quals, declarator);
8004169689Skan      else
8005169689Skan	declarator = make_reference_declarator (cv_quals, declarator);
8006132718Skan
8007169689Skan      return declarator;
8008132718Skan   }
8009132718Skan
8010169689Skan  return NULL;
8011132718Skan}
8012132718Skan
8013132718Skan/* Parse an (optional) ctor-initializer.
8014132718Skan
8015132718Skan   ctor-initializer:
8016169689Skan     : mem-initializer-list
8017132718Skan
8018132718Skan   Returns TRUE iff the ctor-initializer was actually present.  */
8019132718Skan
8020132718Skanstatic bool
8021132718Skancp_parser_ctor_initializer_opt (cp_parser* parser)
8022132718Skan{
8023132718Skan  /* If the next token is not a `:', then there is no
8024132718Skan     ctor-initializer.  */
8025132718Skan  if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
8026132718Skan    {
8027132718Skan      /* Do default initialization of any bases and members.  */
8028132718Skan      if (DECL_CONSTRUCTOR_P (current_function_decl))
8029132718Skan	finish_mem_initializers (NULL_TREE);
8030132718Skan
8031132718Skan      return false;
8032132718Skan    }
8033132718Skan
8034132718Skan  /* Consume the `:' token.  */
8035132718Skan  cp_lexer_consume_token (parser->lexer);
8036132718Skan  /* And the mem-initializer-list.  */
8037132718Skan  cp_parser_mem_initializer_list (parser);
8038132718Skan
8039132718Skan  return true;
8040132718Skan}
8041132718Skan
8042132718Skan/* Parse a mem-initializer-list.
8043132718Skan
8044132718Skan   mem-initializer-list:
8045132718Skan     mem-initializer
8046132718Skan     mem-initializer , mem-initializer-list  */
8047132718Skan
8048132718Skanstatic void
8049132718Skancp_parser_mem_initializer_list (cp_parser* parser)
8050132718Skan{
8051132718Skan  tree mem_initializer_list = NULL_TREE;
8052132718Skan
8053132718Skan  /* Let the semantic analysis code know that we are starting the
8054132718Skan     mem-initializer-list.  */
8055132718Skan  if (!DECL_CONSTRUCTOR_P (current_function_decl))
8056132718Skan    error ("only constructors take base initializers");
8057132718Skan
8058132718Skan  /* Loop through the list.  */
8059132718Skan  while (true)
8060132718Skan    {
8061132718Skan      tree mem_initializer;
8062132718Skan
8063132718Skan      /* Parse the mem-initializer.  */
8064132718Skan      mem_initializer = cp_parser_mem_initializer (parser);
8065132718Skan      /* Add it to the list, unless it was erroneous.  */
8066169689Skan      if (mem_initializer != error_mark_node)
8067132718Skan	{
8068132718Skan	  TREE_CHAIN (mem_initializer) = mem_initializer_list;
8069132718Skan	  mem_initializer_list = mem_initializer;
8070132718Skan	}
8071132718Skan      /* If the next token is not a `,', we're done.  */
8072132718Skan      if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8073132718Skan	break;
8074132718Skan      /* Consume the `,' token.  */
8075132718Skan      cp_lexer_consume_token (parser->lexer);
8076132718Skan    }
8077132718Skan
8078132718Skan  /* Perform semantic analysis.  */
8079132718Skan  if (DECL_CONSTRUCTOR_P (current_function_decl))
8080132718Skan    finish_mem_initializers (mem_initializer_list);
8081132718Skan}
8082132718Skan
8083132718Skan/* Parse a mem-initializer.
8084132718Skan
8085132718Skan   mem-initializer:
8086169689Skan     mem-initializer-id ( expression-list [opt] )
8087132718Skan
8088132718Skan   GNU extension:
8089169689Skan
8090132718Skan   mem-initializer:
8091132718Skan     ( expression-list [opt] )
8092132718Skan
8093132718Skan   Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
8094132718Skan   class) or FIELD_DECL (for a non-static data member) to initialize;
8095169689Skan   the TREE_VALUE is the expression-list.  An empty initialization
8096169689Skan   list is represented by void_list_node.  */
8097132718Skan
8098132718Skanstatic tree
8099132718Skancp_parser_mem_initializer (cp_parser* parser)
8100132718Skan{
8101132718Skan  tree mem_initializer_id;
8102132718Skan  tree expression_list;
8103132718Skan  tree member;
8104169689Skan
8105132718Skan  /* Find out what is being initialized.  */
8106132718Skan  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8107132718Skan    {
8108132718Skan      pedwarn ("anachronistic old-style base class initializer");
8109132718Skan      mem_initializer_id = NULL_TREE;
8110132718Skan    }
8111132718Skan  else
8112132718Skan    mem_initializer_id = cp_parser_mem_initializer_id (parser);
8113132718Skan  member = expand_member_init (mem_initializer_id);
8114132718Skan  if (member && !DECL_P (member))
8115132718Skan    in_base_initializer = 1;
8116132718Skan
8117169689Skan  expression_list
8118132718Skan    = cp_parser_parenthesized_expression_list (parser, false,
8119169689Skan					       /*cast_p=*/false,
8120132718Skan					       /*non_constant_p=*/NULL);
8121169689Skan  if (expression_list == error_mark_node)
8122169689Skan    return error_mark_node;
8123132718Skan  if (!expression_list)
8124132718Skan    expression_list = void_type_node;
8125132718Skan
8126132718Skan  in_base_initializer = 0;
8127169689Skan
8128169689Skan  return member ? build_tree_list (member, expression_list) : error_mark_node;
8129132718Skan}
8130132718Skan
8131132718Skan/* Parse a mem-initializer-id.
8132132718Skan
8133132718Skan   mem-initializer-id:
8134132718Skan     :: [opt] nested-name-specifier [opt] class-name
8135169689Skan     identifier
8136132718Skan
8137132718Skan   Returns a TYPE indicating the class to be initializer for the first
8138132718Skan   production.  Returns an IDENTIFIER_NODE indicating the data member
8139132718Skan   to be initialized for the second production.  */
8140132718Skan
8141132718Skanstatic tree
8142132718Skancp_parser_mem_initializer_id (cp_parser* parser)
8143132718Skan{
8144132718Skan  bool global_scope_p;
8145132718Skan  bool nested_name_specifier_p;
8146132718Skan  bool template_p = false;
8147132718Skan  tree id;
8148132718Skan
8149132718Skan  /* `typename' is not allowed in this context ([temp.res]).  */
8150132718Skan  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
8151132718Skan    {
8152169689Skan      error ("keyword %<typename%> not allowed in this context (a qualified "
8153132718Skan	     "member initializer is implicitly a type)");
8154132718Skan      cp_lexer_consume_token (parser->lexer);
8155132718Skan    }
8156132718Skan  /* Look for the optional `::' operator.  */
8157169689Skan  global_scope_p
8158169689Skan    = (cp_parser_global_scope_opt (parser,
8159169689Skan				   /*current_scope_valid_p=*/false)
8160132718Skan       != NULL_TREE);
8161132718Skan  /* Look for the optional nested-name-specifier.  The simplest way to
8162132718Skan     implement:
8163132718Skan
8164132718Skan       [temp.res]
8165132718Skan
8166132718Skan       The keyword `typename' is not permitted in a base-specifier or
8167132718Skan       mem-initializer; in these contexts a qualified name that
8168132718Skan       depends on a template-parameter is implicitly assumed to be a
8169132718Skan       type name.
8170132718Skan
8171132718Skan     is to assume that we have seen the `typename' keyword at this
8172132718Skan     point.  */
8173169689Skan  nested_name_specifier_p
8174132718Skan    = (cp_parser_nested_name_specifier_opt (parser,
8175132718Skan					    /*typename_keyword_p=*/true,
8176132718Skan					    /*check_dependency_p=*/true,
8177132718Skan					    /*type_p=*/true,
8178132718Skan					    /*is_declaration=*/true)
8179132718Skan       != NULL_TREE);
8180132718Skan  if (nested_name_specifier_p)
8181132718Skan    template_p = cp_parser_optional_template_keyword (parser);
8182132718Skan  /* If there is a `::' operator or a nested-name-specifier, then we
8183132718Skan     are definitely looking for a class-name.  */
8184132718Skan  if (global_scope_p || nested_name_specifier_p)
8185132718Skan    return cp_parser_class_name (parser,
8186132718Skan				 /*typename_keyword_p=*/true,
8187132718Skan				 /*template_keyword_p=*/template_p,
8188169689Skan				 none_type,
8189132718Skan				 /*check_dependency_p=*/true,
8190132718Skan				 /*class_head_p=*/false,
8191132718Skan				 /*is_declaration=*/true);
8192132718Skan  /* Otherwise, we could also be looking for an ordinary identifier.  */
8193132718Skan  cp_parser_parse_tentatively (parser);
8194132718Skan  /* Try a class-name.  */
8195169689Skan  id = cp_parser_class_name (parser,
8196132718Skan			     /*typename_keyword_p=*/true,
8197132718Skan			     /*template_keyword_p=*/false,
8198169689Skan			     none_type,
8199132718Skan			     /*check_dependency_p=*/true,
8200132718Skan			     /*class_head_p=*/false,
8201132718Skan			     /*is_declaration=*/true);
8202132718Skan  /* If we found one, we're done.  */
8203132718Skan  if (cp_parser_parse_definitely (parser))
8204132718Skan    return id;
8205132718Skan  /* Otherwise, look for an ordinary identifier.  */
8206132718Skan  return cp_parser_identifier (parser);
8207132718Skan}
8208132718Skan
8209132718Skan/* Overloading [gram.over] */
8210132718Skan
8211132718Skan/* Parse an operator-function-id.
8212132718Skan
8213132718Skan   operator-function-id:
8214169689Skan     operator operator
8215132718Skan
8216132718Skan   Returns an IDENTIFIER_NODE for the operator which is a
8217132718Skan   human-readable spelling of the identifier, e.g., `operator +'.  */
8218132718Skan
8219169689Skanstatic tree
8220132718Skancp_parser_operator_function_id (cp_parser* parser)
8221132718Skan{
8222132718Skan  /* Look for the `operator' keyword.  */
8223132718Skan  if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
8224132718Skan    return error_mark_node;
8225132718Skan  /* And then the name of the operator itself.  */
8226132718Skan  return cp_parser_operator (parser);
8227132718Skan}
8228132718Skan
8229132718Skan/* Parse an operator.
8230132718Skan
8231132718Skan   operator:
8232132718Skan     new delete new[] delete[] + - * / % ^ & | ~ ! = < >
8233132718Skan     += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
8234132718Skan     || ++ -- , ->* -> () []
8235132718Skan
8236132718Skan   GNU Extensions:
8237169689Skan
8238132718Skan   operator:
8239132718Skan     <? >? <?= >?=
8240132718Skan
8241132718Skan   Returns an IDENTIFIER_NODE for the operator which is a
8242132718Skan   human-readable spelling of the identifier, e.g., `operator +'.  */
8243169689Skan
8244132718Skanstatic tree
8245132718Skancp_parser_operator (cp_parser* parser)
8246132718Skan{
8247132718Skan  tree id = NULL_TREE;
8248132718Skan  cp_token *token;
8249132718Skan
8250132718Skan  /* Peek at the next token.  */
8251132718Skan  token = cp_lexer_peek_token (parser->lexer);
8252132718Skan  /* Figure out which operator we have.  */
8253132718Skan  switch (token->type)
8254132718Skan    {
8255132718Skan    case CPP_KEYWORD:
8256132718Skan      {
8257132718Skan	enum tree_code op;
8258132718Skan
8259132718Skan	/* The keyword should be either `new' or `delete'.  */
8260132718Skan	if (token->keyword == RID_NEW)
8261132718Skan	  op = NEW_EXPR;
8262132718Skan	else if (token->keyword == RID_DELETE)
8263132718Skan	  op = DELETE_EXPR;
8264132718Skan	else
8265132718Skan	  break;
8266132718Skan
8267132718Skan	/* Consume the `new' or `delete' token.  */
8268132718Skan	cp_lexer_consume_token (parser->lexer);
8269132718Skan
8270132718Skan	/* Peek at the next token.  */
8271132718Skan	token = cp_lexer_peek_token (parser->lexer);
8272132718Skan	/* If it's a `[' token then this is the array variant of the
8273132718Skan	   operator.  */
8274132718Skan	if (token->type == CPP_OPEN_SQUARE)
8275132718Skan	  {
8276132718Skan	    /* Consume the `[' token.  */
8277132718Skan	    cp_lexer_consume_token (parser->lexer);
8278132718Skan	    /* Look for the `]' token.  */
8279132718Skan	    cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8280169689Skan	    id = ansi_opname (op == NEW_EXPR
8281132718Skan			      ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
8282132718Skan	  }
8283132718Skan	/* Otherwise, we have the non-array variant.  */
8284132718Skan	else
8285132718Skan	  id = ansi_opname (op);
8286132718Skan
8287132718Skan	return id;
8288132718Skan      }
8289132718Skan
8290132718Skan    case CPP_PLUS:
8291132718Skan      id = ansi_opname (PLUS_EXPR);
8292132718Skan      break;
8293132718Skan
8294132718Skan    case CPP_MINUS:
8295132718Skan      id = ansi_opname (MINUS_EXPR);
8296132718Skan      break;
8297132718Skan
8298132718Skan    case CPP_MULT:
8299132718Skan      id = ansi_opname (MULT_EXPR);
8300132718Skan      break;
8301132718Skan
8302132718Skan    case CPP_DIV:
8303132718Skan      id = ansi_opname (TRUNC_DIV_EXPR);
8304132718Skan      break;
8305132718Skan
8306132718Skan    case CPP_MOD:
8307132718Skan      id = ansi_opname (TRUNC_MOD_EXPR);
8308132718Skan      break;
8309132718Skan
8310132718Skan    case CPP_XOR:
8311132718Skan      id = ansi_opname (BIT_XOR_EXPR);
8312132718Skan      break;
8313132718Skan
8314132718Skan    case CPP_AND:
8315132718Skan      id = ansi_opname (BIT_AND_EXPR);
8316132718Skan      break;
8317132718Skan
8318132718Skan    case CPP_OR:
8319132718Skan      id = ansi_opname (BIT_IOR_EXPR);
8320132718Skan      break;
8321132718Skan
8322132718Skan    case CPP_COMPL:
8323132718Skan      id = ansi_opname (BIT_NOT_EXPR);
8324132718Skan      break;
8325169689Skan
8326132718Skan    case CPP_NOT:
8327132718Skan      id = ansi_opname (TRUTH_NOT_EXPR);
8328132718Skan      break;
8329132718Skan
8330132718Skan    case CPP_EQ:
8331132718Skan      id = ansi_assopname (NOP_EXPR);
8332132718Skan      break;
8333132718Skan
8334132718Skan    case CPP_LESS:
8335132718Skan      id = ansi_opname (LT_EXPR);
8336132718Skan      break;
8337132718Skan
8338132718Skan    case CPP_GREATER:
8339132718Skan      id = ansi_opname (GT_EXPR);
8340132718Skan      break;
8341132718Skan
8342132718Skan    case CPP_PLUS_EQ:
8343132718Skan      id = ansi_assopname (PLUS_EXPR);
8344132718Skan      break;
8345132718Skan
8346132718Skan    case CPP_MINUS_EQ:
8347132718Skan      id = ansi_assopname (MINUS_EXPR);
8348132718Skan      break;
8349132718Skan
8350132718Skan    case CPP_MULT_EQ:
8351132718Skan      id = ansi_assopname (MULT_EXPR);
8352132718Skan      break;
8353132718Skan
8354132718Skan    case CPP_DIV_EQ:
8355132718Skan      id = ansi_assopname (TRUNC_DIV_EXPR);
8356132718Skan      break;
8357132718Skan
8358132718Skan    case CPP_MOD_EQ:
8359132718Skan      id = ansi_assopname (TRUNC_MOD_EXPR);
8360132718Skan      break;
8361132718Skan
8362132718Skan    case CPP_XOR_EQ:
8363132718Skan      id = ansi_assopname (BIT_XOR_EXPR);
8364132718Skan      break;
8365132718Skan
8366132718Skan    case CPP_AND_EQ:
8367132718Skan      id = ansi_assopname (BIT_AND_EXPR);
8368132718Skan      break;
8369132718Skan
8370132718Skan    case CPP_OR_EQ:
8371132718Skan      id = ansi_assopname (BIT_IOR_EXPR);
8372132718Skan      break;
8373132718Skan
8374132718Skan    case CPP_LSHIFT:
8375132718Skan      id = ansi_opname (LSHIFT_EXPR);
8376132718Skan      break;
8377132718Skan
8378132718Skan    case CPP_RSHIFT:
8379132718Skan      id = ansi_opname (RSHIFT_EXPR);
8380132718Skan      break;
8381132718Skan
8382132718Skan    case CPP_LSHIFT_EQ:
8383132718Skan      id = ansi_assopname (LSHIFT_EXPR);
8384132718Skan      break;
8385132718Skan
8386132718Skan    case CPP_RSHIFT_EQ:
8387132718Skan      id = ansi_assopname (RSHIFT_EXPR);
8388132718Skan      break;
8389132718Skan
8390132718Skan    case CPP_EQ_EQ:
8391132718Skan      id = ansi_opname (EQ_EXPR);
8392132718Skan      break;
8393132718Skan
8394132718Skan    case CPP_NOT_EQ:
8395132718Skan      id = ansi_opname (NE_EXPR);
8396132718Skan      break;
8397132718Skan
8398132718Skan    case CPP_LESS_EQ:
8399132718Skan      id = ansi_opname (LE_EXPR);
8400132718Skan      break;
8401132718Skan
8402132718Skan    case CPP_GREATER_EQ:
8403132718Skan      id = ansi_opname (GE_EXPR);
8404132718Skan      break;
8405132718Skan
8406132718Skan    case CPP_AND_AND:
8407132718Skan      id = ansi_opname (TRUTH_ANDIF_EXPR);
8408132718Skan      break;
8409132718Skan
8410132718Skan    case CPP_OR_OR:
8411132718Skan      id = ansi_opname (TRUTH_ORIF_EXPR);
8412132718Skan      break;
8413169689Skan
8414132718Skan    case CPP_PLUS_PLUS:
8415132718Skan      id = ansi_opname (POSTINCREMENT_EXPR);
8416132718Skan      break;
8417132718Skan
8418132718Skan    case CPP_MINUS_MINUS:
8419132718Skan      id = ansi_opname (PREDECREMENT_EXPR);
8420132718Skan      break;
8421132718Skan
8422132718Skan    case CPP_COMMA:
8423132718Skan      id = ansi_opname (COMPOUND_EXPR);
8424132718Skan      break;
8425132718Skan
8426132718Skan    case CPP_DEREF_STAR:
8427132718Skan      id = ansi_opname (MEMBER_REF);
8428132718Skan      break;
8429132718Skan
8430132718Skan    case CPP_DEREF:
8431132718Skan      id = ansi_opname (COMPONENT_REF);
8432132718Skan      break;
8433132718Skan
8434132718Skan    case CPP_OPEN_PAREN:
8435132718Skan      /* Consume the `('.  */
8436132718Skan      cp_lexer_consume_token (parser->lexer);
8437132718Skan      /* Look for the matching `)'.  */
8438132718Skan      cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
8439132718Skan      return ansi_opname (CALL_EXPR);
8440132718Skan
8441132718Skan    case CPP_OPEN_SQUARE:
8442132718Skan      /* Consume the `['.  */
8443132718Skan      cp_lexer_consume_token (parser->lexer);
8444132718Skan      /* Look for the matching `]'.  */
8445132718Skan      cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8446132718Skan      return ansi_opname (ARRAY_REF);
8447132718Skan
8448132718Skan    default:
8449132718Skan      /* Anything else is an error.  */
8450132718Skan      break;
8451132718Skan    }
8452132718Skan
8453132718Skan  /* If we have selected an identifier, we need to consume the
8454132718Skan     operator token.  */
8455132718Skan  if (id)
8456132718Skan    cp_lexer_consume_token (parser->lexer);
8457132718Skan  /* Otherwise, no valid operator name was present.  */
8458132718Skan  else
8459132718Skan    {
8460132718Skan      cp_parser_error (parser, "expected operator");
8461132718Skan      id = error_mark_node;
8462132718Skan    }
8463132718Skan
8464132718Skan  return id;
8465132718Skan}
8466132718Skan
8467132718Skan/* Parse a template-declaration.
8468132718Skan
8469132718Skan   template-declaration:
8470169689Skan     export [opt] template < template-parameter-list > declaration
8471132718Skan
8472132718Skan   If MEMBER_P is TRUE, this template-declaration occurs within a
8473169689Skan   class-specifier.
8474132718Skan
8475132718Skan   The grammar rule given by the standard isn't correct.  What
8476132718Skan   is really meant is:
8477132718Skan
8478132718Skan   template-declaration:
8479169689Skan     export [opt] template-parameter-list-seq
8480132718Skan       decl-specifier-seq [opt] init-declarator [opt] ;
8481169689Skan     export [opt] template-parameter-list-seq
8482132718Skan       function-definition
8483132718Skan
8484132718Skan   template-parameter-list-seq:
8485132718Skan     template-parameter-list-seq [opt]
8486132718Skan     template < template-parameter-list >  */
8487132718Skan
8488132718Skanstatic void
8489132718Skancp_parser_template_declaration (cp_parser* parser, bool member_p)
8490132718Skan{
8491132718Skan  /* Check for `export'.  */
8492132718Skan  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
8493132718Skan    {
8494132718Skan      /* Consume the `export' token.  */
8495132718Skan      cp_lexer_consume_token (parser->lexer);
8496132718Skan      /* Warn that we do not support `export'.  */
8497169689Skan      warning (0, "keyword %<export%> not implemented, and will be ignored");
8498132718Skan    }
8499132718Skan
8500132718Skan  cp_parser_template_declaration_after_export (parser, member_p);
8501132718Skan}
8502132718Skan
8503132718Skan/* Parse a template-parameter-list.
8504132718Skan
8505132718Skan   template-parameter-list:
8506132718Skan     template-parameter
8507132718Skan     template-parameter-list , template-parameter
8508132718Skan
8509132718Skan   Returns a TREE_LIST.  Each node represents a template parameter.
8510132718Skan   The nodes are connected via their TREE_CHAINs.  */
8511132718Skan
8512132718Skanstatic tree
8513132718Skancp_parser_template_parameter_list (cp_parser* parser)
8514132718Skan{
8515132718Skan  tree parameter_list = NULL_TREE;
8516132718Skan
8517169689Skan  begin_template_parm_list ();
8518132718Skan  while (true)
8519132718Skan    {
8520132718Skan      tree parameter;
8521132718Skan      cp_token *token;
8522169689Skan      bool is_non_type;
8523132718Skan
8524132718Skan      /* Parse the template-parameter.  */
8525169689Skan      parameter = cp_parser_template_parameter (parser, &is_non_type);
8526132718Skan      /* Add it to the list.  */
8527169689Skan      if (parameter != error_mark_node)
8528169689Skan	parameter_list = process_template_parm (parameter_list,
8529169689Skan						parameter,
8530169689Skan						is_non_type);
8531169689Skan      else
8532169689Skan       {
8533169689Skan         tree err_parm = build_tree_list (parameter, parameter);
8534169689Skan         TREE_VALUE (err_parm) = error_mark_node;
8535169689Skan         parameter_list = chainon (parameter_list, err_parm);
8536169689Skan       }
8537132718Skan
8538132718Skan      /* Peek at the next token.  */
8539132718Skan      token = cp_lexer_peek_token (parser->lexer);
8540132718Skan      /* If it's not a `,', we're done.  */
8541132718Skan      if (token->type != CPP_COMMA)
8542132718Skan	break;
8543132718Skan      /* Otherwise, consume the `,' token.  */
8544132718Skan      cp_lexer_consume_token (parser->lexer);
8545132718Skan    }
8546132718Skan
8547169689Skan  return end_template_parm_list (parameter_list);
8548132718Skan}
8549132718Skan
8550132718Skan/* Parse a template-parameter.
8551132718Skan
8552132718Skan   template-parameter:
8553132718Skan     type-parameter
8554132718Skan     parameter-declaration
8555132718Skan
8556169689Skan   If all goes well, returns a TREE_LIST.  The TREE_VALUE represents
8557169689Skan   the parameter.  The TREE_PURPOSE is the default value, if any.
8558169689Skan   Returns ERROR_MARK_NODE on failure.  *IS_NON_TYPE is set to true
8559169689Skan   iff this parameter is a non-type parameter.  */
8560132718Skan
8561132718Skanstatic tree
8562169689Skancp_parser_template_parameter (cp_parser* parser, bool *is_non_type)
8563132718Skan{
8564132718Skan  cp_token *token;
8565169689Skan  cp_parameter_declarator *parameter_declarator;
8566169689Skan  tree parm;
8567132718Skan
8568169689Skan  /* Assume it is a type parameter or a template parameter.  */
8569169689Skan  *is_non_type = false;
8570132718Skan  /* Peek at the next token.  */
8571132718Skan  token = cp_lexer_peek_token (parser->lexer);
8572132718Skan  /* If it is `class' or `template', we have a type-parameter.  */
8573132718Skan  if (token->keyword == RID_TEMPLATE)
8574132718Skan    return cp_parser_type_parameter (parser);
8575132718Skan  /* If it is `class' or `typename' we do not know yet whether it is a
8576132718Skan     type parameter or a non-type parameter.  Consider:
8577132718Skan
8578132718Skan       template <typename T, typename T::X X> ...
8579132718Skan
8580132718Skan     or:
8581169689Skan
8582132718Skan       template <class C, class D*> ...
8583132718Skan
8584132718Skan     Here, the first parameter is a type parameter, and the second is
8585132718Skan     a non-type parameter.  We can tell by looking at the token after
8586132718Skan     the identifier -- if it is a `,', `=', or `>' then we have a type
8587132718Skan     parameter.  */
8588132718Skan  if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
8589132718Skan    {
8590132718Skan      /* Peek at the token after `class' or `typename'.  */
8591132718Skan      token = cp_lexer_peek_nth_token (parser->lexer, 2);
8592132718Skan      /* If it's an identifier, skip it.  */
8593132718Skan      if (token->type == CPP_NAME)
8594132718Skan	token = cp_lexer_peek_nth_token (parser->lexer, 3);
8595132718Skan      /* Now, see if the token looks like the end of a template
8596132718Skan	 parameter.  */
8597169689Skan      if (token->type == CPP_COMMA
8598132718Skan	  || token->type == CPP_EQ
8599132718Skan	  || token->type == CPP_GREATER)
8600132718Skan	return cp_parser_type_parameter (parser);
8601132718Skan    }
8602132718Skan
8603169689Skan  /* Otherwise, it is a non-type parameter.
8604132718Skan
8605132718Skan     [temp.param]
8606132718Skan
8607132718Skan     When parsing a default template-argument for a non-type
8608132718Skan     template-parameter, the first non-nested `>' is taken as the end
8609132718Skan     of the template parameter-list rather than a greater-than
8610132718Skan     operator.  */
8611169689Skan  *is_non_type = true;
8612169689Skan  parameter_declarator
8613169689Skan     = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
8614169689Skan					/*parenthesized_p=*/NULL);
8615169689Skan  parm = grokdeclarator (parameter_declarator->declarator,
8616169689Skan			 &parameter_declarator->decl_specifiers,
8617169689Skan			 PARM, /*initialized=*/0,
8618169689Skan			 /*attrlist=*/NULL);
8619169689Skan  if (parm == error_mark_node)
8620169689Skan    return error_mark_node;
8621169689Skan  return build_tree_list (parameter_declarator->default_argument, parm);
8622132718Skan}
8623132718Skan
8624132718Skan/* Parse a type-parameter.
8625132718Skan
8626132718Skan   type-parameter:
8627132718Skan     class identifier [opt]
8628132718Skan     class identifier [opt] = type-id
8629132718Skan     typename identifier [opt]
8630132718Skan     typename identifier [opt] = type-id
8631132718Skan     template < template-parameter-list > class identifier [opt]
8632169689Skan     template < template-parameter-list > class identifier [opt]
8633169689Skan       = id-expression
8634132718Skan
8635132718Skan   Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
8636132718Skan   TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
8637132718Skan   the declaration of the parameter.  */
8638132718Skan
8639132718Skanstatic tree
8640132718Skancp_parser_type_parameter (cp_parser* parser)
8641132718Skan{
8642132718Skan  cp_token *token;
8643132718Skan  tree parameter;
8644132718Skan
8645132718Skan  /* Look for a keyword to tell us what kind of parameter this is.  */
8646169689Skan  token = cp_parser_require (parser, CPP_KEYWORD,
8647132718Skan			     "`class', `typename', or `template'");
8648132718Skan  if (!token)
8649132718Skan    return error_mark_node;
8650132718Skan
8651132718Skan  switch (token->keyword)
8652132718Skan    {
8653132718Skan    case RID_CLASS:
8654132718Skan    case RID_TYPENAME:
8655132718Skan      {
8656132718Skan	tree identifier;
8657132718Skan	tree default_argument;
8658132718Skan
8659132718Skan	/* If the next token is an identifier, then it names the
8660169689Skan	   parameter.  */
8661132718Skan	if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8662132718Skan	  identifier = cp_parser_identifier (parser);
8663132718Skan	else
8664132718Skan	  identifier = NULL_TREE;
8665132718Skan
8666132718Skan	/* Create the parameter.  */
8667132718Skan	parameter = finish_template_type_parm (class_type_node, identifier);
8668132718Skan
8669132718Skan	/* If the next token is an `=', we have a default argument.  */
8670132718Skan	if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8671132718Skan	  {
8672132718Skan	    /* Consume the `=' token.  */
8673132718Skan	    cp_lexer_consume_token (parser->lexer);
8674132718Skan	    /* Parse the default-argument.  */
8675169689Skan	    push_deferring_access_checks (dk_no_deferred);
8676132718Skan	    default_argument = cp_parser_type_id (parser);
8677169689Skan	    pop_deferring_access_checks ();
8678132718Skan	  }
8679132718Skan	else
8680132718Skan	  default_argument = NULL_TREE;
8681132718Skan
8682132718Skan	/* Create the combined representation of the parameter and the
8683132718Skan	   default argument.  */
8684132718Skan	parameter = build_tree_list (default_argument, parameter);
8685132718Skan      }
8686132718Skan      break;
8687132718Skan
8688132718Skan    case RID_TEMPLATE:
8689132718Skan      {
8690132718Skan	tree parameter_list;
8691132718Skan	tree identifier;
8692132718Skan	tree default_argument;
8693132718Skan
8694132718Skan	/* Look for the `<'.  */
8695132718Skan	cp_parser_require (parser, CPP_LESS, "`<'");
8696132718Skan	/* Parse the template-parameter-list.  */
8697169689Skan	parameter_list = cp_parser_template_parameter_list (parser);
8698132718Skan	/* Look for the `>'.  */
8699132718Skan	cp_parser_require (parser, CPP_GREATER, "`>'");
8700132718Skan	/* Look for the `class' keyword.  */
8701132718Skan	cp_parser_require_keyword (parser, RID_CLASS, "`class'");
8702132718Skan	/* If the next token is an `=', then there is a
8703132718Skan	   default-argument.  If the next token is a `>', we are at
8704132718Skan	   the end of the parameter-list.  If the next token is a `,',
8705132718Skan	   then we are at the end of this parameter.  */
8706132718Skan	if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8707132718Skan	    && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
8708132718Skan	    && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8709161651Skan	  {
8710161651Skan	    identifier = cp_parser_identifier (parser);
8711169689Skan	    /* Treat invalid names as if the parameter were nameless.  */
8712161651Skan	    if (identifier == error_mark_node)
8713161651Skan	      identifier = NULL_TREE;
8714161651Skan	  }
8715132718Skan	else
8716132718Skan	  identifier = NULL_TREE;
8717161651Skan
8718132718Skan	/* Create the template parameter.  */
8719132718Skan	parameter = finish_template_template_parm (class_type_node,
8720132718Skan						   identifier);
8721169689Skan
8722132718Skan	/* If the next token is an `=', then there is a
8723132718Skan	   default-argument.  */
8724132718Skan	if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8725132718Skan	  {
8726132718Skan	    bool is_template;
8727132718Skan
8728132718Skan	    /* Consume the `='.  */
8729132718Skan	    cp_lexer_consume_token (parser->lexer);
8730132718Skan	    /* Parse the id-expression.  */
8731169689Skan	    push_deferring_access_checks (dk_no_deferred);
8732169689Skan	    default_argument
8733132718Skan	      = cp_parser_id_expression (parser,
8734132718Skan					 /*template_keyword_p=*/false,
8735132718Skan					 /*check_dependency_p=*/true,
8736132718Skan					 /*template_p=*/&is_template,
8737169689Skan					 /*declarator_p=*/false,
8738169689Skan					 /*optional_p=*/false);
8739132718Skan	    if (TREE_CODE (default_argument) == TYPE_DECL)
8740132718Skan	      /* If the id-expression was a template-id that refers to
8741132718Skan		 a template-class, we already have the declaration here,
8742132718Skan		 so no further lookup is needed.  */
8743132718Skan		 ;
8744132718Skan	    else
8745132718Skan	      /* Look up the name.  */
8746169689Skan	      default_argument
8747132718Skan		= cp_parser_lookup_name (parser, default_argument,
8748169689Skan					 none_type,
8749169689Skan					 /*is_template=*/is_template,
8750169689Skan					 /*is_namespace=*/false,
8751169689Skan					 /*check_dependency=*/true,
8752169689Skan					 /*ambiguous_decls=*/NULL);
8753132718Skan	    /* See if the default argument is valid.  */
8754132718Skan	    default_argument
8755132718Skan	      = check_template_template_default_arg (default_argument);
8756169689Skan	    pop_deferring_access_checks ();
8757132718Skan	  }
8758132718Skan	else
8759132718Skan	  default_argument = NULL_TREE;
8760132718Skan
8761132718Skan	/* Create the combined representation of the parameter and the
8762132718Skan	   default argument.  */
8763161651Skan	parameter = build_tree_list (default_argument, parameter);
8764132718Skan      }
8765132718Skan      break;
8766132718Skan
8767132718Skan    default:
8768169689Skan      gcc_unreachable ();
8769161651Skan      break;
8770132718Skan    }
8771169689Skan
8772132718Skan  return parameter;
8773132718Skan}
8774132718Skan
8775132718Skan/* Parse a template-id.
8776132718Skan
8777132718Skan   template-id:
8778132718Skan     template-name < template-argument-list [opt] >
8779132718Skan
8780132718Skan   If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
8781132718Skan   `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
8782132718Skan   returned.  Otherwise, if the template-name names a function, or set
8783132718Skan   of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
8784169689Skan   names a class, returns a TYPE_DECL for the specialization.
8785132718Skan
8786132718Skan   If CHECK_DEPENDENCY_P is FALSE, names are looked up in
8787132718Skan   uninstantiated templates.  */
8788132718Skan
8789132718Skanstatic tree
8790169689Skancp_parser_template_id (cp_parser *parser,
8791169689Skan		       bool template_keyword_p,
8792132718Skan		       bool check_dependency_p,
8793132718Skan		       bool is_declaration)
8794132718Skan{
8795169689Skan  int i;
8796132718Skan  tree template;
8797132718Skan  tree arguments;
8798132718Skan  tree template_id;
8799169689Skan  cp_token_position start_of_id = 0;
8800169689Skan  deferred_access_check *chk;
8801169689Skan  VEC (deferred_access_check,gc) *access_check;
8802132718Skan  cp_token *next_token, *next_token_2;
8803132718Skan  bool is_identifier;
8804132718Skan
8805132718Skan  /* If the next token corresponds to a template-id, there is no need
8806132718Skan     to reparse it.  */
8807132718Skan  next_token = cp_lexer_peek_token (parser->lexer);
8808132718Skan  if (next_token->type == CPP_TEMPLATE_ID)
8809132718Skan    {
8810169689Skan      struct tree_check *check_value;
8811132718Skan
8812132718Skan      /* Get the stored value.  */
8813169689Skan      check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
8814132718Skan      /* Perform any access checks that were deferred.  */
8815169689Skan      access_check = check_value->checks;
8816169689Skan      if (access_check)
8817169689Skan	{
8818169689Skan	  for (i = 0 ;
8819169689Skan	       VEC_iterate (deferred_access_check, access_check, i, chk) ;
8820169689Skan	       ++i)
8821169689Skan	    {
8822169689Skan	      perform_or_defer_access_check (chk->binfo,
8823169689Skan					     chk->decl,
8824169689Skan					     chk->diag_decl);
8825169689Skan	    }
8826169689Skan	}
8827132718Skan      /* Return the stored value.  */
8828169689Skan      return check_value->value;
8829132718Skan    }
8830132718Skan
8831132718Skan  /* Avoid performing name lookup if there is no possibility of
8832132718Skan     finding a template-id.  */
8833132718Skan  if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
8834132718Skan      || (next_token->type == CPP_NAME
8835169689Skan	  && !cp_parser_nth_token_starts_template_argument_list_p
8836132718Skan	       (parser, 2)))
8837132718Skan    {
8838132718Skan      cp_parser_error (parser, "expected template-id");
8839132718Skan      return error_mark_node;
8840132718Skan    }
8841132718Skan
8842132718Skan  /* Remember where the template-id starts.  */
8843169689Skan  if (cp_parser_uncommitted_to_tentative_parse_p (parser))
8844169689Skan    start_of_id = cp_lexer_token_position (parser->lexer, false);
8845132718Skan
8846132718Skan  push_deferring_access_checks (dk_deferred);
8847132718Skan
8848132718Skan  /* Parse the template-name.  */
8849132718Skan  is_identifier = false;
8850132718Skan  template = cp_parser_template_name (parser, template_keyword_p,
8851132718Skan				      check_dependency_p,
8852132718Skan				      is_declaration,
8853132718Skan				      &is_identifier);
8854132718Skan  if (template == error_mark_node || is_identifier)
8855132718Skan    {
8856132718Skan      pop_deferring_access_checks ();
8857132718Skan      return template;
8858132718Skan    }
8859132718Skan
8860169689Skan  /* If we find the sequence `[:' after a template-name, it's probably
8861132718Skan     a digraph-typo for `< ::'. Substitute the tokens and check if we can
8862132718Skan     parse correctly the argument list.  */
8863169689Skan  next_token = cp_lexer_peek_token (parser->lexer);
8864132718Skan  next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8865169689Skan  if (next_token->type == CPP_OPEN_SQUARE
8866132718Skan      && next_token->flags & DIGRAPH
8867169689Skan      && next_token_2->type == CPP_COLON
8868132718Skan      && !(next_token_2->flags & PREV_WHITE))
8869132718Skan    {
8870132718Skan      cp_parser_parse_tentatively (parser);
8871132718Skan      /* Change `:' into `::'.  */
8872132718Skan      next_token_2->type = CPP_SCOPE;
8873132718Skan      /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
8874169689Skan	 CPP_LESS.  */
8875132718Skan      cp_lexer_consume_token (parser->lexer);
8876132718Skan      /* Parse the arguments.  */
8877132718Skan      arguments = cp_parser_enclosed_template_argument_list (parser);
8878132718Skan      if (!cp_parser_parse_definitely (parser))
8879132718Skan	{
8880132718Skan	  /* If we couldn't parse an argument list, then we revert our changes
8881132718Skan	     and return simply an error. Maybe this is not a template-id
8882132718Skan	     after all.  */
8883132718Skan	  next_token_2->type = CPP_COLON;
8884169689Skan	  cp_parser_error (parser, "expected %<<%>");
8885132718Skan	  pop_deferring_access_checks ();
8886132718Skan	  return error_mark_node;
8887132718Skan	}
8888132718Skan      /* Otherwise, emit an error about the invalid digraph, but continue
8889169689Skan	 parsing because we got our argument list.  */
8890169689Skan      pedwarn ("%<<::%> cannot begin a template-argument list");
8891169689Skan      inform ("%<<:%> is an alternate spelling for %<[%>. Insert whitespace "
8892169689Skan	      "between %<<%> and %<::%>");
8893132718Skan      if (!flag_permissive)
8894132718Skan	{
8895132718Skan	  static bool hint;
8896132718Skan	  if (!hint)
8897132718Skan	    {
8898169689Skan	      inform ("(if you use -fpermissive G++ will accept your code)");
8899132718Skan	      hint = true;
8900132718Skan	    }
8901132718Skan	}
8902132718Skan    }
8903132718Skan  else
8904132718Skan    {
8905132718Skan      /* Look for the `<' that starts the template-argument-list.  */
8906132718Skan      if (!cp_parser_require (parser, CPP_LESS, "`<'"))
8907132718Skan	{
8908132718Skan	  pop_deferring_access_checks ();
8909132718Skan	  return error_mark_node;
8910132718Skan	}
8911132718Skan      /* Parse the arguments.  */
8912132718Skan      arguments = cp_parser_enclosed_template_argument_list (parser);
8913132718Skan    }
8914132718Skan
8915132718Skan  /* Build a representation of the specialization.  */
8916132718Skan  if (TREE_CODE (template) == IDENTIFIER_NODE)
8917132718Skan    template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
8918132718Skan  else if (DECL_CLASS_TEMPLATE_P (template)
8919132718Skan	   || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
8920169689Skan    {
8921169689Skan      bool entering_scope;
8922169689Skan      /* In "template <typename T> ... A<T>::", A<T> is the abstract A
8923169689Skan	 template (rather than some instantiation thereof) only if
8924169689Skan	 is not nested within some other construct.  For example, in
8925169689Skan	 "template <typename T> void f(T) { A<T>::", A<T> is just an
8926169689Skan	 instantiation of A.  */
8927169689Skan      entering_scope = (template_parm_scope_p ()
8928169689Skan			&& cp_lexer_next_token_is (parser->lexer,
8929169689Skan						   CPP_SCOPE));
8930169689Skan      template_id
8931169689Skan	= finish_template_type (template, arguments, entering_scope);
8932169689Skan    }
8933132718Skan  else
8934132718Skan    {
8935132718Skan      /* If it's not a class-template or a template-template, it should be
8936132718Skan	 a function-template.  */
8937169689Skan      gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8938169689Skan		   || TREE_CODE (template) == OVERLOAD
8939169689Skan		   || BASELINK_P (template)));
8940169689Skan
8941132718Skan      template_id = lookup_template_function (template, arguments);
8942132718Skan    }
8943132718Skan
8944132718Skan  /* If parsing tentatively, replace the sequence of tokens that makes
8945132718Skan     up the template-id with a CPP_TEMPLATE_ID token.  That way,
8946132718Skan     should we re-parse the token stream, we will not have to repeat
8947132718Skan     the effort required to do the parse, nor will we issue duplicate
8948132718Skan     error messages about problems during instantiation of the
8949132718Skan     template.  */
8950169689Skan  if (start_of_id)
8951132718Skan    {
8952169689Skan      cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
8953132718Skan
8954132718Skan      /* Reset the contents of the START_OF_ID token.  */
8955132718Skan      token->type = CPP_TEMPLATE_ID;
8956169689Skan      /* Retrieve any deferred checks.  Do not pop this access checks yet
8957169689Skan	 so the memory will not be reclaimed during token replacing below.  */
8958169689Skan      token->u.tree_check_value = GGC_CNEW (struct tree_check);
8959169689Skan      token->u.tree_check_value->value = template_id;
8960169689Skan      token->u.tree_check_value->checks = get_deferred_access_checks ();
8961132718Skan      token->keyword = RID_MAX;
8962169689Skan
8963132718Skan      /* Purge all subsequent tokens.  */
8964169689Skan      cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
8965146895Skan
8966146895Skan      /* ??? Can we actually assume that, if template_id ==
8967146895Skan	 error_mark_node, we will have issued a diagnostic to the
8968146895Skan	 user, as opposed to simply marking the tentative parse as
8969146895Skan	 failed?  */
8970146895Skan      if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
8971146895Skan	error ("parse error in template argument list");
8972132718Skan    }
8973132718Skan
8974132718Skan  pop_deferring_access_checks ();
8975132718Skan  return template_id;
8976132718Skan}
8977132718Skan
8978132718Skan/* Parse a template-name.
8979132718Skan
8980132718Skan   template-name:
8981132718Skan     identifier
8982169689Skan
8983132718Skan   The standard should actually say:
8984132718Skan
8985132718Skan   template-name:
8986132718Skan     identifier
8987132718Skan     operator-function-id
8988132718Skan
8989132718Skan   A defect report has been filed about this issue.
8990132718Skan
8991132718Skan   A conversion-function-id cannot be a template name because they cannot
8992132718Skan   be part of a template-id. In fact, looking at this code:
8993132718Skan
8994132718Skan   a.operator K<int>()
8995132718Skan
8996132718Skan   the conversion-function-id is "operator K<int>", and K<int> is a type-id.
8997169689Skan   It is impossible to call a templated conversion-function-id with an
8998132718Skan   explicit argument list, since the only allowed template parameter is
8999132718Skan   the type to which it is converting.
9000132718Skan
9001132718Skan   If TEMPLATE_KEYWORD_P is true, then we have just seen the
9002132718Skan   `template' keyword, in a construction like:
9003132718Skan
9004132718Skan     T::template f<3>()
9005132718Skan
9006132718Skan   In that case `f' is taken to be a template-name, even though there
9007132718Skan   is no way of knowing for sure.
9008132718Skan
9009132718Skan   Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
9010132718Skan   name refers to a set of overloaded functions, at least one of which
9011132718Skan   is a template, or an IDENTIFIER_NODE with the name of the template,
9012132718Skan   if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
9013132718Skan   names are looked up inside uninstantiated templates.  */
9014132718Skan
9015132718Skanstatic tree
9016169689Skancp_parser_template_name (cp_parser* parser,
9017169689Skan			 bool template_keyword_p,
9018169689Skan			 bool check_dependency_p,
9019132718Skan			 bool is_declaration,
9020132718Skan			 bool *is_identifier)
9021132718Skan{
9022132718Skan  tree identifier;
9023132718Skan  tree decl;
9024132718Skan  tree fns;
9025132718Skan
9026132718Skan  /* If the next token is `operator', then we have either an
9027132718Skan     operator-function-id or a conversion-function-id.  */
9028132718Skan  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
9029132718Skan    {
9030132718Skan      /* We don't know whether we're looking at an
9031132718Skan	 operator-function-id or a conversion-function-id.  */
9032132718Skan      cp_parser_parse_tentatively (parser);
9033132718Skan      /* Try an operator-function-id.  */
9034132718Skan      identifier = cp_parser_operator_function_id (parser);
9035132718Skan      /* If that didn't work, try a conversion-function-id.  */
9036132718Skan      if (!cp_parser_parse_definitely (parser))
9037169689Skan	{
9038132718Skan	  cp_parser_error (parser, "expected template-name");
9039132718Skan	  return error_mark_node;
9040169689Skan	}
9041132718Skan    }
9042132718Skan  /* Look for the identifier.  */
9043132718Skan  else
9044132718Skan    identifier = cp_parser_identifier (parser);
9045169689Skan
9046132718Skan  /* If we didn't find an identifier, we don't have a template-id.  */
9047132718Skan  if (identifier == error_mark_node)
9048132718Skan    return error_mark_node;
9049132718Skan
9050132718Skan  /* If the name immediately followed the `template' keyword, then it
9051132718Skan     is a template-name.  However, if the next token is not `<', then
9052132718Skan     we do not treat it as a template-name, since it is not being used
9053132718Skan     as part of a template-id.  This enables us to handle constructs
9054132718Skan     like:
9055132718Skan
9056132718Skan       template <typename T> struct S { S(); };
9057132718Skan       template <typename T> S<T>::S();
9058132718Skan
9059132718Skan     correctly.  We would treat `S' as a template -- if it were `S<T>'
9060132718Skan     -- but we do not if there is no `<'.  */
9061132718Skan
9062132718Skan  if (processing_template_decl
9063132718Skan      && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
9064132718Skan    {
9065132718Skan      /* In a declaration, in a dependent context, we pretend that the
9066132718Skan	 "template" keyword was present in order to improve error
9067132718Skan	 recovery.  For example, given:
9068169689Skan
9069132718Skan	   template <typename T> void f(T::X<int>);
9070169689Skan
9071132718Skan	 we want to treat "X<int>" as a template-id.  */
9072169689Skan      if (is_declaration
9073169689Skan	  && !template_keyword_p
9074132718Skan	  && parser->scope && TYPE_P (parser->scope)
9075146895Skan	  && check_dependency_p
9076132718Skan	  && dependent_type_p (parser->scope)
9077132718Skan	  /* Do not do this for dtors (or ctors), since they never
9078132718Skan	     need the template keyword before their name.  */
9079132718Skan	  && !constructor_name_p (identifier, parser->scope))
9080132718Skan	{
9081169689Skan	  cp_token_position start = 0;
9082169689Skan
9083132718Skan	  /* Explain what went wrong.  */
9084169689Skan	  error ("non-template %qD used as template", identifier);
9085169689Skan	  inform ("use %<%T::template %D%> to indicate that it is a template",
9086132718Skan		  parser->scope, identifier);
9087169689Skan	  /* If parsing tentatively, find the location of the "<" token.  */
9088169689Skan	  if (cp_parser_simulate_error (parser))
9089169689Skan	    start = cp_lexer_token_position (parser->lexer, true);
9090132718Skan	  /* Parse the template arguments so that we can issue error
9091132718Skan	     messages about them.  */
9092132718Skan	  cp_lexer_consume_token (parser->lexer);
9093132718Skan	  cp_parser_enclosed_template_argument_list (parser);
9094132718Skan	  /* Skip tokens until we find a good place from which to
9095132718Skan	     continue parsing.  */
9096132718Skan	  cp_parser_skip_to_closing_parenthesis (parser,
9097132718Skan						 /*recovering=*/true,
9098132718Skan						 /*or_comma=*/true,
9099132718Skan						 /*consume_paren=*/false);
9100132718Skan	  /* If parsing tentatively, permanently remove the
9101132718Skan	     template argument list.  That will prevent duplicate
9102132718Skan	     error messages from being issued about the missing
9103132718Skan	     "template" keyword.  */
9104169689Skan	  if (start)
9105169689Skan	    cp_lexer_purge_tokens_after (parser->lexer, start);
9106132718Skan	  if (is_identifier)
9107132718Skan	    *is_identifier = true;
9108132718Skan	  return identifier;
9109132718Skan	}
9110132718Skan
9111132718Skan      /* If the "template" keyword is present, then there is generally
9112132718Skan	 no point in doing name-lookup, so we just return IDENTIFIER.
9113132718Skan	 But, if the qualifying scope is non-dependent then we can
9114132718Skan	 (and must) do name-lookup normally.  */
9115132718Skan      if (template_keyword_p
9116132718Skan	  && (!parser->scope
9117169689Skan	      || (TYPE_P (parser->scope)
9118132718Skan		  && dependent_type_p (parser->scope))))
9119132718Skan	return identifier;
9120132718Skan    }
9121132718Skan
9122132718Skan  /* Look up the name.  */
9123132718Skan  decl = cp_parser_lookup_name (parser, identifier,
9124169689Skan				none_type,
9125132718Skan				/*is_template=*/false,
9126132718Skan				/*is_namespace=*/false,
9127169689Skan				check_dependency_p,
9128169689Skan				/*ambiguous_decls=*/NULL);
9129132718Skan  decl = maybe_get_template_decl_from_type_decl (decl);
9130132718Skan
9131132718Skan  /* If DECL is a template, then the name was a template-name.  */
9132132718Skan  if (TREE_CODE (decl) == TEMPLATE_DECL)
9133132718Skan    ;
9134169689Skan  else
9135132718Skan    {
9136146895Skan      tree fn = NULL_TREE;
9137146895Skan
9138132718Skan      /* The standard does not explicitly indicate whether a name that
9139132718Skan	 names a set of overloaded declarations, some of which are
9140132718Skan	 templates, is a template-name.  However, such a name should
9141132718Skan	 be a template-name; otherwise, there is no way to form a
9142132718Skan	 template-id for the overloaded templates.  */
9143132718Skan      fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
9144132718Skan      if (TREE_CODE (fns) == OVERLOAD)
9145146895Skan	for (fn = fns; fn; fn = OVL_NEXT (fn))
9146146895Skan	  if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
9147146895Skan	    break;
9148146895Skan
9149146895Skan      if (!fn)
9150132718Skan	{
9151169689Skan	  /* The name does not name a template.  */
9152132718Skan	  cp_parser_error (parser, "expected template-name");
9153132718Skan	  return error_mark_node;
9154132718Skan	}
9155132718Skan    }
9156132718Skan
9157132718Skan  /* If DECL is dependent, and refers to a function, then just return
9158132718Skan     its name; we will look it up again during template instantiation.  */
9159132718Skan  if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
9160132718Skan    {
9161132718Skan      tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
9162132718Skan      if (TYPE_P (scope) && dependent_type_p (scope))
9163132718Skan	return identifier;
9164132718Skan    }
9165132718Skan
9166132718Skan  return decl;
9167132718Skan}
9168132718Skan
9169132718Skan/* Parse a template-argument-list.
9170132718Skan
9171132718Skan   template-argument-list:
9172132718Skan     template-argument
9173132718Skan     template-argument-list , template-argument
9174132718Skan
9175132718Skan   Returns a TREE_VEC containing the arguments.  */
9176132718Skan
9177132718Skanstatic tree
9178132718Skancp_parser_template_argument_list (cp_parser* parser)
9179132718Skan{
9180132718Skan  tree fixed_args[10];
9181132718Skan  unsigned n_args = 0;
9182132718Skan  unsigned alloced = 10;
9183132718Skan  tree *arg_ary = fixed_args;
9184132718Skan  tree vec;
9185132718Skan  bool saved_in_template_argument_list_p;
9186169689Skan  bool saved_ice_p;
9187169689Skan  bool saved_non_ice_p;
9188132718Skan
9189132718Skan  saved_in_template_argument_list_p = parser->in_template_argument_list_p;
9190132718Skan  parser->in_template_argument_list_p = true;
9191169689Skan  /* Even if the template-id appears in an integral
9192169689Skan     constant-expression, the contents of the argument list do
9193169689Skan     not.  */
9194169689Skan  saved_ice_p = parser->integral_constant_expression_p;
9195169689Skan  parser->integral_constant_expression_p = false;
9196169689Skan  saved_non_ice_p = parser->non_integral_constant_expression_p;
9197169689Skan  parser->non_integral_constant_expression_p = false;
9198169689Skan  /* Parse the arguments.  */
9199132718Skan  do
9200132718Skan    {
9201132718Skan      tree argument;
9202132718Skan
9203132718Skan      if (n_args)
9204132718Skan	/* Consume the comma.  */
9205132718Skan	cp_lexer_consume_token (parser->lexer);
9206169689Skan
9207132718Skan      /* Parse the template-argument.  */
9208132718Skan      argument = cp_parser_template_argument (parser);
9209132718Skan      if (n_args == alloced)
9210132718Skan	{
9211132718Skan	  alloced *= 2;
9212169689Skan
9213132718Skan	  if (arg_ary == fixed_args)
9214132718Skan	    {
9215169689Skan	      arg_ary = XNEWVEC (tree, alloced);
9216132718Skan	      memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
9217132718Skan	    }
9218132718Skan	  else
9219169689Skan	    arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
9220132718Skan	}
9221132718Skan      arg_ary[n_args++] = argument;
9222132718Skan    }
9223132718Skan  while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
9224132718Skan
9225132718Skan  vec = make_tree_vec (n_args);
9226132718Skan
9227132718Skan  while (n_args--)
9228132718Skan    TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
9229169689Skan
9230132718Skan  if (arg_ary != fixed_args)
9231132718Skan    free (arg_ary);
9232169689Skan  parser->non_integral_constant_expression_p = saved_non_ice_p;
9233169689Skan  parser->integral_constant_expression_p = saved_ice_p;
9234132718Skan  parser->in_template_argument_list_p = saved_in_template_argument_list_p;
9235132718Skan  return vec;
9236132718Skan}
9237132718Skan
9238132718Skan/* Parse a template-argument.
9239132718Skan
9240132718Skan   template-argument:
9241132718Skan     assignment-expression
9242132718Skan     type-id
9243132718Skan     id-expression
9244132718Skan
9245132718Skan   The representation is that of an assignment-expression, type-id, or
9246132718Skan   id-expression -- except that the qualified id-expression is
9247132718Skan   evaluated, so that the value returned is either a DECL or an
9248169689Skan   OVERLOAD.
9249132718Skan
9250132718Skan   Although the standard says "assignment-expression", it forbids
9251132718Skan   throw-expressions or assignments in the template argument.
9252132718Skan   Therefore, we use "conditional-expression" instead.  */
9253132718Skan
9254132718Skanstatic tree
9255132718Skancp_parser_template_argument (cp_parser* parser)
9256132718Skan{
9257132718Skan  tree argument;
9258132718Skan  bool template_p;
9259132718Skan  bool address_p;
9260132718Skan  bool maybe_type_id = false;
9261132718Skan  cp_token *token;
9262132718Skan  cp_id_kind idk;
9263132718Skan
9264132718Skan  /* There's really no way to know what we're looking at, so we just
9265169689Skan     try each alternative in order.
9266132718Skan
9267132718Skan       [temp.arg]
9268132718Skan
9269132718Skan       In a template-argument, an ambiguity between a type-id and an
9270132718Skan       expression is resolved to a type-id, regardless of the form of
9271169689Skan       the corresponding template-parameter.
9272132718Skan
9273132718Skan     Therefore, we try a type-id first.  */
9274132718Skan  cp_parser_parse_tentatively (parser);
9275132718Skan  argument = cp_parser_type_id (parser);
9276132718Skan  /* If there was no error parsing the type-id but the next token is a '>>',
9277169689Skan     we probably found a typo for '> >'. But there are type-id which are
9278132718Skan     also valid expressions. For instance:
9279132718Skan
9280132718Skan     struct X { int operator >> (int); };
9281132718Skan     template <int V> struct Foo {};
9282132718Skan     Foo<X () >> 5> r;
9283132718Skan
9284132718Skan     Here 'X()' is a valid type-id of a function type, but the user just
9285132718Skan     wanted to write the expression "X() >> 5". Thus, we remember that we
9286132718Skan     found a valid type-id, but we still try to parse the argument as an
9287132718Skan     expression to see what happens.  */
9288132718Skan  if (!cp_parser_error_occurred (parser)
9289132718Skan      && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
9290132718Skan    {
9291132718Skan      maybe_type_id = true;
9292132718Skan      cp_parser_abort_tentative_parse (parser);
9293132718Skan    }
9294132718Skan  else
9295132718Skan    {
9296132718Skan      /* If the next token isn't a `,' or a `>', then this argument wasn't
9297132718Skan      really finished. This means that the argument is not a valid
9298132718Skan      type-id.  */
9299132718Skan      if (!cp_parser_next_token_ends_template_argument_p (parser))
9300132718Skan	cp_parser_error (parser, "expected template-argument");
9301132718Skan      /* If that worked, we're done.  */
9302132718Skan      if (cp_parser_parse_definitely (parser))
9303132718Skan	return argument;
9304132718Skan    }
9305132718Skan  /* We're still not sure what the argument will be.  */
9306132718Skan  cp_parser_parse_tentatively (parser);
9307132718Skan  /* Try a template.  */
9308169689Skan  argument = cp_parser_id_expression (parser,
9309132718Skan				      /*template_keyword_p=*/false,
9310132718Skan				      /*check_dependency_p=*/true,
9311132718Skan				      &template_p,
9312169689Skan				      /*declarator_p=*/false,
9313169689Skan				      /*optional_p=*/false);
9314132718Skan  /* If the next token isn't a `,' or a `>', then this argument wasn't
9315132718Skan     really finished.  */
9316132718Skan  if (!cp_parser_next_token_ends_template_argument_p (parser))
9317132718Skan    cp_parser_error (parser, "expected template-argument");
9318132718Skan  if (!cp_parser_error_occurred (parser))
9319132718Skan    {
9320132718Skan      /* Figure out what is being referred to.  If the id-expression
9321132718Skan	 was for a class template specialization, then we will have a
9322132718Skan	 TYPE_DECL at this point.  There is no need to do name lookup
9323132718Skan	 at this point in that case.  */
9324132718Skan      if (TREE_CODE (argument) != TYPE_DECL)
9325132718Skan	argument = cp_parser_lookup_name (parser, argument,
9326169689Skan					  none_type,
9327132718Skan					  /*is_template=*/template_p,
9328132718Skan					  /*is_namespace=*/false,
9329169689Skan					  /*check_dependency=*/true,
9330169689Skan					  /*ambiguous_decls=*/NULL);
9331132718Skan      if (TREE_CODE (argument) != TEMPLATE_DECL
9332132718Skan	  && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
9333132718Skan	cp_parser_error (parser, "expected template-name");
9334132718Skan    }
9335132718Skan  if (cp_parser_parse_definitely (parser))
9336132718Skan    return argument;
9337132718Skan  /* It must be a non-type argument.  There permitted cases are given
9338132718Skan     in [temp.arg.nontype]:
9339132718Skan
9340132718Skan     -- an integral constant-expression of integral or enumeration
9341169689Skan	type; or
9342132718Skan
9343132718Skan     -- the name of a non-type template-parameter; or
9344132718Skan
9345132718Skan     -- the name of an object or function with external linkage...
9346132718Skan
9347132718Skan     -- the address of an object or function with external linkage...
9348132718Skan
9349132718Skan     -- a pointer to member...  */
9350132718Skan  /* Look for a non-type template parameter.  */
9351132718Skan  if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9352132718Skan    {
9353132718Skan      cp_parser_parse_tentatively (parser);
9354132718Skan      argument = cp_parser_primary_expression (parser,
9355169689Skan					       /*adress_p=*/false,
9356169689Skan					       /*cast_p=*/false,
9357169689Skan					       /*template_arg_p=*/true,
9358169689Skan					       &idk);
9359132718Skan      if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
9360132718Skan	  || !cp_parser_next_token_ends_template_argument_p (parser))
9361132718Skan	cp_parser_simulate_error (parser);
9362132718Skan      if (cp_parser_parse_definitely (parser))
9363132718Skan	return argument;
9364132718Skan    }
9365169689Skan
9366132718Skan  /* If the next token is "&", the argument must be the address of an
9367132718Skan     object or function with external linkage.  */
9368132718Skan  address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
9369132718Skan  if (address_p)
9370132718Skan    cp_lexer_consume_token (parser->lexer);
9371132718Skan  /* See if we might have an id-expression.  */
9372132718Skan  token = cp_lexer_peek_token (parser->lexer);
9373132718Skan  if (token->type == CPP_NAME
9374132718Skan      || token->keyword == RID_OPERATOR
9375132718Skan      || token->type == CPP_SCOPE
9376132718Skan      || token->type == CPP_TEMPLATE_ID
9377132718Skan      || token->type == CPP_NESTED_NAME_SPECIFIER)
9378132718Skan    {
9379132718Skan      cp_parser_parse_tentatively (parser);
9380132718Skan      argument = cp_parser_primary_expression (parser,
9381169689Skan					       address_p,
9382169689Skan					       /*cast_p=*/false,
9383169689Skan					       /*template_arg_p=*/true,
9384169689Skan					       &idk);
9385132718Skan      if (cp_parser_error_occurred (parser)
9386132718Skan	  || !cp_parser_next_token_ends_template_argument_p (parser))
9387132718Skan	cp_parser_abort_tentative_parse (parser);
9388132718Skan      else
9389132718Skan	{
9390169689Skan	  if (TREE_CODE (argument) == INDIRECT_REF)
9391169689Skan	    {
9392169689Skan	      gcc_assert (REFERENCE_REF_P (argument));
9393169689Skan	      argument = TREE_OPERAND (argument, 0);
9394169689Skan	    }
9395169689Skan
9396132718Skan	  if (TREE_CODE (argument) == VAR_DECL)
9397132718Skan	    {
9398132718Skan	      /* A variable without external linkage might still be a
9399132718Skan		 valid constant-expression, so no error is issued here
9400132718Skan		 if the external-linkage check fails.  */
9401169689Skan	      if (!address_p && !DECL_EXTERNAL_LINKAGE_P (argument))
9402132718Skan		cp_parser_simulate_error (parser);
9403132718Skan	    }
9404132718Skan	  else if (is_overloaded_fn (argument))
9405132718Skan	    /* All overloaded functions are allowed; if the external
9406132718Skan	       linkage test does not pass, an error will be issued
9407132718Skan	       later.  */
9408132718Skan	    ;
9409132718Skan	  else if (address_p
9410169689Skan		   && (TREE_CODE (argument) == OFFSET_REF
9411132718Skan		       || TREE_CODE (argument) == SCOPE_REF))
9412132718Skan	    /* A pointer-to-member.  */
9413132718Skan	    ;
9414169689Skan	  else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
9415169689Skan	    ;
9416132718Skan	  else
9417132718Skan	    cp_parser_simulate_error (parser);
9418132718Skan
9419132718Skan	  if (cp_parser_parse_definitely (parser))
9420132718Skan	    {
9421132718Skan	      if (address_p)
9422132718Skan		argument = build_x_unary_op (ADDR_EXPR, argument);
9423132718Skan	      return argument;
9424132718Skan	    }
9425132718Skan	}
9426132718Skan    }
9427132718Skan  /* If the argument started with "&", there are no other valid
9428132718Skan     alternatives at this point.  */
9429132718Skan  if (address_p)
9430132718Skan    {
9431132718Skan      cp_parser_error (parser, "invalid non-type template argument");
9432132718Skan      return error_mark_node;
9433132718Skan    }
9434169689Skan
9435132718Skan  /* If the argument wasn't successfully parsed as a type-id followed
9436169689Skan     by '>>', the argument can only be a constant expression now.
9437132718Skan     Otherwise, we try parsing the constant-expression tentatively,
9438132718Skan     because the argument could really be a type-id.  */
9439132718Skan  if (maybe_type_id)
9440132718Skan    cp_parser_parse_tentatively (parser);
9441169689Skan  argument = cp_parser_constant_expression (parser,
9442132718Skan					    /*allow_non_constant_p=*/false,
9443132718Skan					    /*non_constant_p=*/NULL);
9444132718Skan  argument = fold_non_dependent_expr (argument);
9445132718Skan  if (!maybe_type_id)
9446132718Skan    return argument;
9447132718Skan  if (!cp_parser_next_token_ends_template_argument_p (parser))
9448132718Skan    cp_parser_error (parser, "expected template-argument");
9449132718Skan  if (cp_parser_parse_definitely (parser))
9450132718Skan    return argument;
9451132718Skan  /* We did our best to parse the argument as a non type-id, but that
9452132718Skan     was the only alternative that matched (albeit with a '>' after
9453169689Skan     it). We can assume it's just a typo from the user, and a
9454132718Skan     diagnostic will then be issued.  */
9455132718Skan  return cp_parser_type_id (parser);
9456132718Skan}
9457132718Skan
9458132718Skan/* Parse an explicit-instantiation.
9459132718Skan
9460132718Skan   explicit-instantiation:
9461169689Skan     template declaration
9462132718Skan
9463132718Skan   Although the standard says `declaration', what it really means is:
9464132718Skan
9465132718Skan   explicit-instantiation:
9466169689Skan     template decl-specifier-seq [opt] declarator [opt] ;
9467132718Skan
9468132718Skan   Things like `template int S<int>::i = 5, int S<double>::j;' are not
9469132718Skan   supposed to be allowed.  A defect report has been filed about this
9470169689Skan   issue.
9471132718Skan
9472132718Skan   GNU Extension:
9473169689Skan
9474132718Skan   explicit-instantiation:
9475169689Skan     storage-class-specifier template
9476132718Skan       decl-specifier-seq [opt] declarator [opt] ;
9477169689Skan     function-specifier template
9478132718Skan       decl-specifier-seq [opt] declarator [opt] ;  */
9479132718Skan
9480132718Skanstatic void
9481132718Skancp_parser_explicit_instantiation (cp_parser* parser)
9482132718Skan{
9483132718Skan  int declares_class_or_enum;
9484169689Skan  cp_decl_specifier_seq decl_specifiers;
9485132718Skan  tree extension_specifier = NULL_TREE;
9486132718Skan
9487132718Skan  /* Look for an (optional) storage-class-specifier or
9488132718Skan     function-specifier.  */
9489132718Skan  if (cp_parser_allow_gnu_extensions_p (parser))
9490132718Skan    {
9491169689Skan      extension_specifier
9492132718Skan	= cp_parser_storage_class_specifier_opt (parser);
9493132718Skan      if (!extension_specifier)
9494169689Skan	extension_specifier
9495169689Skan	  = cp_parser_function_specifier_opt (parser,
9496169689Skan					      /*decl_specs=*/NULL);
9497132718Skan    }
9498132718Skan
9499132718Skan  /* Look for the `template' keyword.  */
9500132718Skan  cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9501132718Skan  /* Let the front end know that we are processing an explicit
9502132718Skan     instantiation.  */
9503132718Skan  begin_explicit_instantiation ();
9504132718Skan  /* [temp.explicit] says that we are supposed to ignore access
9505132718Skan     control while processing explicit instantiation directives.  */
9506132718Skan  push_deferring_access_checks (dk_no_check);
9507132718Skan  /* Parse a decl-specifier-seq.  */
9508169689Skan  cp_parser_decl_specifier_seq (parser,
9509169689Skan				CP_PARSER_FLAGS_OPTIONAL,
9510169689Skan				&decl_specifiers,
9511169689Skan				&declares_class_or_enum);
9512132718Skan  /* If there was exactly one decl-specifier, and it declared a class,
9513132718Skan     and there's no declarator, then we have an explicit type
9514132718Skan     instantiation.  */
9515132718Skan  if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
9516132718Skan    {
9517132718Skan      tree type;
9518132718Skan
9519169689Skan      type = check_tag_decl (&decl_specifiers);
9520132718Skan      /* Turn access control back on for names used during
9521132718Skan	 template instantiation.  */
9522132718Skan      pop_deferring_access_checks ();
9523132718Skan      if (type)
9524169689Skan	do_type_instantiation (type, extension_specifier,
9525169689Skan			       /*complain=*/tf_error);
9526132718Skan    }
9527132718Skan  else
9528132718Skan    {
9529169689Skan      cp_declarator *declarator;
9530132718Skan      tree decl;
9531132718Skan
9532132718Skan      /* Parse the declarator.  */
9533169689Skan      declarator
9534132718Skan	= cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9535132718Skan				/*ctor_dtor_or_conv_p=*/NULL,
9536146895Skan				/*parenthesized_p=*/NULL,
9537146895Skan				/*member_p=*/false);
9538161651Skan      if (declares_class_or_enum & 2)
9539169689Skan	cp_parser_check_for_definition_in_return_type (declarator,
9540169689Skan						       decl_specifiers.type);
9541169689Skan      if (declarator != cp_error_declarator)
9542132718Skan	{
9543169689Skan	  decl = grokdeclarator (declarator, &decl_specifiers,
9544169689Skan				 NORMAL, 0, &decl_specifiers.attributes);
9545132718Skan	  /* Turn access control back on for names used during
9546132718Skan	     template instantiation.  */
9547132718Skan	  pop_deferring_access_checks ();
9548132718Skan	  /* Do the explicit instantiation.  */
9549132718Skan	  do_decl_instantiation (decl, extension_specifier);
9550132718Skan	}
9551132718Skan      else
9552132718Skan	{
9553132718Skan	  pop_deferring_access_checks ();
9554132718Skan	  /* Skip the body of the explicit instantiation.  */
9555132718Skan	  cp_parser_skip_to_end_of_statement (parser);
9556132718Skan	}
9557132718Skan    }
9558132718Skan  /* We're done with the instantiation.  */
9559132718Skan  end_explicit_instantiation ();
9560132718Skan
9561132718Skan  cp_parser_consume_semicolon_at_end_of_statement (parser);
9562132718Skan}
9563132718Skan
9564132718Skan/* Parse an explicit-specialization.
9565132718Skan
9566132718Skan   explicit-specialization:
9567169689Skan     template < > declaration
9568132718Skan
9569132718Skan   Although the standard says `declaration', what it really means is:
9570132718Skan
9571132718Skan   explicit-specialization:
9572132718Skan     template <> decl-specifier [opt] init-declarator [opt] ;
9573169689Skan     template <> function-definition
9574132718Skan     template <> explicit-specialization
9575132718Skan     template <> template-declaration  */
9576132718Skan
9577132718Skanstatic void
9578132718Skancp_parser_explicit_specialization (cp_parser* parser)
9579132718Skan{
9580169689Skan  bool need_lang_pop;
9581132718Skan  /* Look for the `template' keyword.  */
9582132718Skan  cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9583132718Skan  /* Look for the `<'.  */
9584132718Skan  cp_parser_require (parser, CPP_LESS, "`<'");
9585132718Skan  /* Look for the `>'.  */
9586132718Skan  cp_parser_require (parser, CPP_GREATER, "`>'");
9587132718Skan  /* We have processed another parameter list.  */
9588132718Skan  ++parser->num_template_parameter_lists;
9589169689Skan  /* [temp]
9590169689Skan
9591169689Skan     A template ... explicit specialization ... shall not have C
9592169689Skan     linkage.  */
9593169689Skan  if (current_lang_name == lang_name_c)
9594169689Skan    {
9595169689Skan      error ("template specialization with C linkage");
9596169689Skan      /* Give it C++ linkage to avoid confusing other parts of the
9597169689Skan	 front end.  */
9598169689Skan      push_lang_context (lang_name_cplusplus);
9599169689Skan      need_lang_pop = true;
9600169689Skan    }
9601169689Skan  else
9602169689Skan    need_lang_pop = false;
9603132718Skan  /* Let the front end know that we are beginning a specialization.  */
9604169689Skan  if (!begin_specialization ())
9605169689Skan    {
9606169689Skan      end_specialization ();
9607169689Skan      cp_parser_skip_to_end_of_block_or_statement (parser);
9608169689Skan      return;
9609169689Skan    }
9610132718Skan
9611132718Skan  /* If the next keyword is `template', we need to figure out whether
9612132718Skan     or not we're looking a template-declaration.  */
9613132718Skan  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
9614132718Skan    {
9615132718Skan      if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
9616132718Skan	  && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
9617132718Skan	cp_parser_template_declaration_after_export (parser,
9618132718Skan						     /*member_p=*/false);
9619132718Skan      else
9620132718Skan	cp_parser_explicit_specialization (parser);
9621132718Skan    }
9622132718Skan  else
9623132718Skan    /* Parse the dependent declaration.  */
9624169689Skan    cp_parser_single_declaration (parser,
9625169689Skan				  /*checks=*/NULL,
9626132718Skan				  /*member_p=*/false,
9627132718Skan				  /*friend_p=*/NULL);
9628132718Skan  /* We're done with the specialization.  */
9629132718Skan  end_specialization ();
9630169689Skan  /* For the erroneous case of a template with C linkage, we pushed an
9631169689Skan     implicit C++ linkage scope; exit that scope now.  */
9632169689Skan  if (need_lang_pop)
9633169689Skan    pop_lang_context ();
9634132718Skan  /* We're done with this parameter list.  */
9635132718Skan  --parser->num_template_parameter_lists;
9636132718Skan}
9637132718Skan
9638132718Skan/* Parse a type-specifier.
9639132718Skan
9640132718Skan   type-specifier:
9641132718Skan     simple-type-specifier
9642132718Skan     class-specifier
9643132718Skan     enum-specifier
9644132718Skan     elaborated-type-specifier
9645132718Skan     cv-qualifier
9646132718Skan
9647132718Skan   GNU Extension:
9648132718Skan
9649132718Skan   type-specifier:
9650132718Skan     __complex__
9651132718Skan
9652169689Skan   Returns a representation of the type-specifier.  For a
9653169689Skan   class-specifier, enum-specifier, or elaborated-type-specifier, a
9654169689Skan   TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
9655132718Skan
9656169689Skan   The parser flags FLAGS is used to control type-specifier parsing.
9657132718Skan
9658169689Skan   If IS_DECLARATION is TRUE, then this type-specifier is appearing
9659169689Skan   in a decl-specifier-seq.
9660169689Skan
9661132718Skan   If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
9662132718Skan   class-specifier, enum-specifier, or elaborated-type-specifier, then
9663132718Skan   *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
9664132718Skan   if a type is declared; 2 if it is defined.  Otherwise, it is set to
9665132718Skan   zero.
9666132718Skan
9667132718Skan   If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
9668132718Skan   cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
9669132718Skan   is set to FALSE.  */
9670132718Skan
9671132718Skanstatic tree
9672169689Skancp_parser_type_specifier (cp_parser* parser,
9673169689Skan			  cp_parser_flags flags,
9674169689Skan			  cp_decl_specifier_seq *decl_specs,
9675132718Skan			  bool is_declaration,
9676132718Skan			  int* declares_class_or_enum,
9677132718Skan			  bool* is_cv_qualifier)
9678132718Skan{
9679132718Skan  tree type_spec = NULL_TREE;
9680132718Skan  cp_token *token;
9681132718Skan  enum rid keyword;
9682169689Skan  cp_decl_spec ds = ds_last;
9683132718Skan
9684132718Skan  /* Assume this type-specifier does not declare a new type.  */
9685132718Skan  if (declares_class_or_enum)
9686132718Skan    *declares_class_or_enum = 0;
9687132718Skan  /* And that it does not specify a cv-qualifier.  */
9688132718Skan  if (is_cv_qualifier)
9689132718Skan    *is_cv_qualifier = false;
9690132718Skan  /* Peek at the next token.  */
9691132718Skan  token = cp_lexer_peek_token (parser->lexer);
9692132718Skan
9693132718Skan  /* If we're looking at a keyword, we can use that to guide the
9694132718Skan     production we choose.  */
9695132718Skan  keyword = token->keyword;
9696132718Skan  switch (keyword)
9697132718Skan    {
9698146895Skan    case RID_ENUM:
9699169689Skan      /* Look for the enum-specifier.  */
9700169689Skan      type_spec = cp_parser_enum_specifier (parser);
9701169689Skan      /* If that worked, we're done.  */
9702169689Skan      if (type_spec)
9703146895Skan	{
9704146895Skan	  if (declares_class_or_enum)
9705146895Skan	    *declares_class_or_enum = 2;
9706169689Skan	  if (decl_specs)
9707169689Skan	    cp_parser_set_decl_spec_type (decl_specs,
9708169689Skan					  type_spec,
9709169689Skan					  /*user_defined_p=*/true);
9710146895Skan	  return type_spec;
9711146895Skan	}
9712146895Skan      else
9713146895Skan	goto elaborated_type_specifier;
9714146895Skan
9715132718Skan      /* Any of these indicate either a class-specifier, or an
9716132718Skan	 elaborated-type-specifier.  */
9717132718Skan    case RID_CLASS:
9718132718Skan    case RID_STRUCT:
9719132718Skan    case RID_UNION:
9720132718Skan      /* Parse tentatively so that we can back up if we don't find a
9721169689Skan	 class-specifier.  */
9722132718Skan      cp_parser_parse_tentatively (parser);
9723146895Skan      /* Look for the class-specifier.  */
9724146895Skan      type_spec = cp_parser_class_specifier (parser);
9725132718Skan      /* If that worked, we're done.  */
9726132718Skan      if (cp_parser_parse_definitely (parser))
9727132718Skan	{
9728132718Skan	  if (declares_class_or_enum)
9729132718Skan	    *declares_class_or_enum = 2;
9730169689Skan	  if (decl_specs)
9731169689Skan	    cp_parser_set_decl_spec_type (decl_specs,
9732169689Skan					  type_spec,
9733169689Skan					  /*user_defined_p=*/true);
9734132718Skan	  return type_spec;
9735132718Skan	}
9736132718Skan
9737132718Skan      /* Fall through.  */
9738169689Skan    elaborated_type_specifier:
9739169689Skan      /* We're declaring (not defining) a class or enum.  */
9740169689Skan      if (declares_class_or_enum)
9741169689Skan	*declares_class_or_enum = 1;
9742132718Skan
9743169689Skan      /* Fall through.  */
9744132718Skan    case RID_TYPENAME:
9745132718Skan      /* Look for an elaborated-type-specifier.  */
9746169689Skan      type_spec
9747169689Skan	= (cp_parser_elaborated_type_specifier
9748169689Skan	   (parser,
9749169689Skan	    decl_specs && decl_specs->specs[(int) ds_friend],
9750169689Skan	    is_declaration));
9751169689Skan      if (decl_specs)
9752169689Skan	cp_parser_set_decl_spec_type (decl_specs,
9753169689Skan				      type_spec,
9754169689Skan				      /*user_defined_p=*/true);
9755132718Skan      return type_spec;
9756132718Skan
9757132718Skan    case RID_CONST:
9758169689Skan      ds = ds_const;
9759169689Skan      if (is_cv_qualifier)
9760169689Skan	*is_cv_qualifier = true;
9761169689Skan      break;
9762169689Skan
9763132718Skan    case RID_VOLATILE:
9764169689Skan      ds = ds_volatile;
9765169689Skan      if (is_cv_qualifier)
9766169689Skan	*is_cv_qualifier = true;
9767169689Skan      break;
9768169689Skan
9769132718Skan    case RID_RESTRICT:
9770169689Skan      ds = ds_restrict;
9771132718Skan      if (is_cv_qualifier)
9772132718Skan	*is_cv_qualifier = true;
9773169689Skan      break;
9774132718Skan
9775132718Skan    case RID_COMPLEX:
9776132718Skan      /* The `__complex__' keyword is a GNU extension.  */
9777169689Skan      ds = ds_complex;
9778169689Skan      break;
9779132718Skan
9780132718Skan    default:
9781132718Skan      break;
9782132718Skan    }
9783132718Skan
9784169689Skan  /* Handle simple keywords.  */
9785169689Skan  if (ds != ds_last)
9786169689Skan    {
9787169689Skan      if (decl_specs)
9788169689Skan	{
9789169689Skan	  ++decl_specs->specs[(int)ds];
9790169689Skan	  decl_specs->any_specifiers_p = true;
9791169689Skan	}
9792169689Skan      return cp_lexer_consume_token (parser->lexer)->u.value;
9793169689Skan    }
9794169689Skan
9795132718Skan  /* If we do not already have a type-specifier, assume we are looking
9796132718Skan     at a simple-type-specifier.  */
9797169689Skan  type_spec = cp_parser_simple_type_specifier (parser,
9798169689Skan					       decl_specs,
9799169689Skan					       flags);
9800132718Skan
9801132718Skan  /* If we didn't find a type-specifier, and a type-specifier was not
9802132718Skan     optional in this context, issue an error message.  */
9803132718Skan  if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9804132718Skan    {
9805132718Skan      cp_parser_error (parser, "expected type specifier");
9806132718Skan      return error_mark_node;
9807132718Skan    }
9808132718Skan
9809132718Skan  return type_spec;
9810132718Skan}
9811132718Skan
9812132718Skan/* Parse a simple-type-specifier.
9813132718Skan
9814132718Skan   simple-type-specifier:
9815132718Skan     :: [opt] nested-name-specifier [opt] type-name
9816132718Skan     :: [opt] nested-name-specifier template template-id
9817132718Skan     char
9818132718Skan     wchar_t
9819132718Skan     bool
9820132718Skan     short
9821132718Skan     int
9822132718Skan     long
9823132718Skan     signed
9824132718Skan     unsigned
9825132718Skan     float
9826132718Skan     double
9827169689Skan     void
9828132718Skan
9829132718Skan   GNU Extension:
9830132718Skan
9831132718Skan   simple-type-specifier:
9832132718Skan     __typeof__ unary-expression
9833132718Skan     __typeof__ ( type-id )
9834132718Skan
9835169689Skan   Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
9836169689Skan   appropriately updated.  */
9837132718Skan
9838132718Skanstatic tree
9839169689Skancp_parser_simple_type_specifier (cp_parser* parser,
9840169689Skan				 cp_decl_specifier_seq *decl_specs,
9841169689Skan				 cp_parser_flags flags)
9842132718Skan{
9843132718Skan  tree type = NULL_TREE;
9844132718Skan  cp_token *token;
9845132718Skan
9846132718Skan  /* Peek at the next token.  */
9847132718Skan  token = cp_lexer_peek_token (parser->lexer);
9848132718Skan
9849132718Skan  /* If we're looking at a keyword, things are easy.  */
9850132718Skan  switch (token->keyword)
9851132718Skan    {
9852132718Skan    case RID_CHAR:
9853169689Skan      if (decl_specs)
9854169689Skan	decl_specs->explicit_char_p = true;
9855132718Skan      type = char_type_node;
9856132718Skan      break;
9857132718Skan    case RID_WCHAR:
9858132718Skan      type = wchar_type_node;
9859132718Skan      break;
9860132718Skan    case RID_BOOL:
9861132718Skan      type = boolean_type_node;
9862132718Skan      break;
9863132718Skan    case RID_SHORT:
9864169689Skan      if (decl_specs)
9865169689Skan	++decl_specs->specs[(int) ds_short];
9866132718Skan      type = short_integer_type_node;
9867132718Skan      break;
9868132718Skan    case RID_INT:
9869169689Skan      if (decl_specs)
9870169689Skan	decl_specs->explicit_int_p = true;
9871132718Skan      type = integer_type_node;
9872132718Skan      break;
9873132718Skan    case RID_LONG:
9874169689Skan      if (decl_specs)
9875169689Skan	++decl_specs->specs[(int) ds_long];
9876132718Skan      type = long_integer_type_node;
9877132718Skan      break;
9878132718Skan    case RID_SIGNED:
9879169689Skan      if (decl_specs)
9880169689Skan	++decl_specs->specs[(int) ds_signed];
9881132718Skan      type = integer_type_node;
9882132718Skan      break;
9883132718Skan    case RID_UNSIGNED:
9884169689Skan      if (decl_specs)
9885169689Skan	++decl_specs->specs[(int) ds_unsigned];
9886132718Skan      type = unsigned_type_node;
9887132718Skan      break;
9888132718Skan    case RID_FLOAT:
9889132718Skan      type = float_type_node;
9890132718Skan      break;
9891132718Skan    case RID_DOUBLE:
9892132718Skan      type = double_type_node;
9893132718Skan      break;
9894132718Skan    case RID_VOID:
9895132718Skan      type = void_type_node;
9896132718Skan      break;
9897132718Skan
9898132718Skan    case RID_TYPEOF:
9899169689Skan      /* Consume the `typeof' token.  */
9900169689Skan      cp_lexer_consume_token (parser->lexer);
9901169689Skan      /* Parse the operand to `typeof'.  */
9902169689Skan      type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
9903169689Skan      /* If it is not already a TYPE, take its type.  */
9904169689Skan      if (!TYPE_P (type))
9905169689Skan	type = finish_typeof (type);
9906132718Skan
9907169689Skan      if (decl_specs)
9908169689Skan	cp_parser_set_decl_spec_type (decl_specs, type,
9909169689Skan				      /*user_defined_p=*/true);
9910132718Skan
9911169689Skan      return type;
9912132718Skan
9913132718Skan    default:
9914132718Skan      break;
9915132718Skan    }
9916132718Skan
9917132718Skan  /* If the type-specifier was for a built-in type, we're done.  */
9918132718Skan  if (type)
9919132718Skan    {
9920132718Skan      tree id;
9921132718Skan
9922169689Skan      /* Record the type.  */
9923169689Skan      if (decl_specs
9924169689Skan	  && (token->keyword != RID_SIGNED
9925169689Skan	      && token->keyword != RID_UNSIGNED
9926169689Skan	      && token->keyword != RID_SHORT
9927169689Skan	      && token->keyword != RID_LONG))
9928169689Skan	cp_parser_set_decl_spec_type (decl_specs,
9929169689Skan				      type,
9930169689Skan				      /*user_defined=*/false);
9931169689Skan      if (decl_specs)
9932169689Skan	decl_specs->any_specifiers_p = true;
9933169689Skan
9934132718Skan      /* Consume the token.  */
9935169689Skan      id = cp_lexer_consume_token (parser->lexer)->u.value;
9936132718Skan
9937132718Skan      /* There is no valid C++ program where a non-template type is
9938132718Skan	 followed by a "<".  That usually indicates that the user thought
9939132718Skan	 that the type was a template.  */
9940132718Skan      cp_parser_check_for_invalid_template_id (parser, type);
9941132718Skan
9942169689Skan      return TYPE_NAME (type);
9943132718Skan    }
9944132718Skan
9945132718Skan  /* The type-specifier must be a user-defined type.  */
9946169689Skan  if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
9947132718Skan    {
9948132718Skan      bool qualified_p;
9949132718Skan      bool global_p;
9950132718Skan
9951132718Skan      /* Don't gobble tokens or issue error messages if this is an
9952132718Skan	 optional type-specifier.  */
9953132718Skan      if (flags & CP_PARSER_FLAGS_OPTIONAL)
9954132718Skan	cp_parser_parse_tentatively (parser);
9955132718Skan
9956132718Skan      /* Look for the optional `::' operator.  */
9957132718Skan      global_p
9958132718Skan	= (cp_parser_global_scope_opt (parser,
9959132718Skan				       /*current_scope_valid_p=*/false)
9960132718Skan	   != NULL_TREE);
9961132718Skan      /* Look for the nested-name specifier.  */
9962132718Skan      qualified_p
9963132718Skan	= (cp_parser_nested_name_specifier_opt (parser,
9964132718Skan						/*typename_keyword_p=*/false,
9965132718Skan						/*check_dependency_p=*/true,
9966132718Skan						/*type_p=*/false,
9967132718Skan						/*is_declaration=*/false)
9968132718Skan	   != NULL_TREE);
9969132718Skan      /* If we have seen a nested-name-specifier, and the next token
9970132718Skan	 is `template', then we are using the template-id production.  */
9971169689Skan      if (parser->scope
9972132718Skan	  && cp_parser_optional_template_keyword (parser))
9973132718Skan	{
9974132718Skan	  /* Look for the template-id.  */
9975169689Skan	  type = cp_parser_template_id (parser,
9976132718Skan					/*template_keyword_p=*/true,
9977132718Skan					/*check_dependency_p=*/true,
9978132718Skan					/*is_declaration=*/false);
9979132718Skan	  /* If the template-id did not name a type, we are out of
9980132718Skan	     luck.  */
9981132718Skan	  if (TREE_CODE (type) != TYPE_DECL)
9982132718Skan	    {
9983132718Skan	      cp_parser_error (parser, "expected template-id for type");
9984132718Skan	      type = NULL_TREE;
9985132718Skan	    }
9986132718Skan	}
9987132718Skan      /* Otherwise, look for a type-name.  */
9988132718Skan      else
9989132718Skan	type = cp_parser_type_name (parser);
9990132718Skan      /* Keep track of all name-lookups performed in class scopes.  */
9991169689Skan      if (type
9992132718Skan	  && !global_p
9993132718Skan	  && !qualified_p
9994169689Skan	  && TREE_CODE (type) == TYPE_DECL
9995132718Skan	  && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
9996132718Skan	maybe_note_name_used_in_class (DECL_NAME (type), type);
9997132718Skan      /* If it didn't work out, we don't have a TYPE.  */
9998169689Skan      if ((flags & CP_PARSER_FLAGS_OPTIONAL)
9999132718Skan	  && !cp_parser_parse_definitely (parser))
10000132718Skan	type = NULL_TREE;
10001169689Skan      if (type && decl_specs)
10002169689Skan	cp_parser_set_decl_spec_type (decl_specs, type,
10003169689Skan				      /*user_defined=*/true);
10004132718Skan    }
10005132718Skan
10006132718Skan  /* If we didn't get a type-name, issue an error message.  */
10007132718Skan  if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
10008132718Skan    {
10009132718Skan      cp_parser_error (parser, "expected type-name");
10010132718Skan      return error_mark_node;
10011132718Skan    }
10012132718Skan
10013132718Skan  /* There is no valid C++ program where a non-template type is
10014132718Skan     followed by a "<".  That usually indicates that the user thought
10015132718Skan     that the type was a template.  */
10016132718Skan  if (type && type != error_mark_node)
10017169689Skan    {
10018169689Skan      /* As a last-ditch effort, see if TYPE is an Objective-C type.
10019169689Skan	 If it is, then the '<'...'>' enclose protocol names rather than
10020169689Skan	 template arguments, and so everything is fine.  */
10021169689Skan      if (c_dialect_objc ()
10022169689Skan	  && (objc_is_id (type) || objc_is_class_name (type)))
10023169689Skan	{
10024169689Skan	  tree protos = cp_parser_objc_protocol_refs_opt (parser);
10025169689Skan	  tree qual_type = objc_get_protocol_qualified_type (type, protos);
10026132718Skan
10027169689Skan	  /* Clobber the "unqualified" type previously entered into
10028169689Skan	     DECL_SPECS with the new, improved protocol-qualified version.  */
10029169689Skan	  if (decl_specs)
10030169689Skan	    decl_specs->type = qual_type;
10031169689Skan
10032169689Skan	  return qual_type;
10033169689Skan	}
10034169689Skan
10035169689Skan      cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
10036169689Skan    }
10037169689Skan
10038132718Skan  return type;
10039132718Skan}
10040132718Skan
10041132718Skan/* Parse a type-name.
10042132718Skan
10043132718Skan   type-name:
10044132718Skan     class-name
10045132718Skan     enum-name
10046169689Skan     typedef-name
10047132718Skan
10048132718Skan   enum-name:
10049132718Skan     identifier
10050132718Skan
10051132718Skan   typedef-name:
10052169689Skan     identifier
10053132718Skan
10054169689Skan   Returns a TYPE_DECL for the type.  */
10055132718Skan
10056132718Skanstatic tree
10057132718Skancp_parser_type_name (cp_parser* parser)
10058132718Skan{
10059132718Skan  tree type_decl;
10060132718Skan  tree identifier;
10061132718Skan
10062132718Skan  /* We can't know yet whether it is a class-name or not.  */
10063132718Skan  cp_parser_parse_tentatively (parser);
10064132718Skan  /* Try a class-name.  */
10065169689Skan  type_decl = cp_parser_class_name (parser,
10066132718Skan				    /*typename_keyword_p=*/false,
10067132718Skan				    /*template_keyword_p=*/false,
10068169689Skan				    none_type,
10069132718Skan				    /*check_dependency_p=*/true,
10070132718Skan				    /*class_head_p=*/false,
10071132718Skan				    /*is_declaration=*/false);
10072132718Skan  /* If it's not a class-name, keep looking.  */
10073132718Skan  if (!cp_parser_parse_definitely (parser))
10074132718Skan    {
10075132718Skan      /* It must be a typedef-name or an enum-name.  */
10076132718Skan      identifier = cp_parser_identifier (parser);
10077132718Skan      if (identifier == error_mark_node)
10078132718Skan	return error_mark_node;
10079169689Skan
10080132718Skan      /* Look up the type-name.  */
10081132718Skan      type_decl = cp_parser_lookup_name_simple (parser, identifier);
10082169689Skan
10083169689Skan      if (TREE_CODE (type_decl) != TYPE_DECL
10084169689Skan	  && (objc_is_id (identifier) || objc_is_class_name (identifier)))
10085169689Skan	{
10086169689Skan	  /* See if this is an Objective-C type.  */
10087169689Skan	  tree protos = cp_parser_objc_protocol_refs_opt (parser);
10088169689Skan	  tree type = objc_get_protocol_qualified_type (identifier, protos);
10089169689Skan	  if (type)
10090169689Skan	    type_decl = TYPE_NAME (type);
10091169689Skan	}
10092169689Skan
10093132718Skan      /* Issue an error if we did not find a type-name.  */
10094132718Skan      if (TREE_CODE (type_decl) != TYPE_DECL)
10095132718Skan	{
10096132718Skan	  if (!cp_parser_simulate_error (parser))
10097169689Skan	    cp_parser_name_lookup_error (parser, identifier, type_decl,
10098132718Skan					 "is not a type");
10099132718Skan	  type_decl = error_mark_node;
10100132718Skan	}
10101132718Skan      /* Remember that the name was used in the definition of the
10102132718Skan	 current class so that we can check later to see if the
10103132718Skan	 meaning would have been different after the class was
10104132718Skan	 entirely defined.  */
10105132718Skan      else if (type_decl != error_mark_node
10106132718Skan	       && !parser->scope)
10107132718Skan	maybe_note_name_used_in_class (identifier, type_decl);
10108132718Skan    }
10109169689Skan
10110132718Skan  return type_decl;
10111132718Skan}
10112132718Skan
10113132718Skan
10114132718Skan/* Parse an elaborated-type-specifier.  Note that the grammar given
10115132718Skan   here incorporates the resolution to DR68.
10116132718Skan
10117132718Skan   elaborated-type-specifier:
10118132718Skan     class-key :: [opt] nested-name-specifier [opt] identifier
10119132718Skan     class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
10120132718Skan     enum :: [opt] nested-name-specifier [opt] identifier
10121132718Skan     typename :: [opt] nested-name-specifier identifier
10122169689Skan     typename :: [opt] nested-name-specifier template [opt]
10123169689Skan       template-id
10124132718Skan
10125132718Skan   GNU extension:
10126132718Skan
10127132718Skan   elaborated-type-specifier:
10128132718Skan     class-key attributes :: [opt] nested-name-specifier [opt] identifier
10129169689Skan     class-key attributes :: [opt] nested-name-specifier [opt]
10130169689Skan	       template [opt] template-id
10131132718Skan     enum attributes :: [opt] nested-name-specifier [opt] identifier
10132132718Skan
10133132718Skan   If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
10134132718Skan   declared `friend'.  If IS_DECLARATION is TRUE, then this
10135132718Skan   elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
10136132718Skan   something is being declared.
10137132718Skan
10138132718Skan   Returns the TYPE specified.  */
10139132718Skan
10140132718Skanstatic tree
10141169689Skancp_parser_elaborated_type_specifier (cp_parser* parser,
10142169689Skan				     bool is_friend,
10143169689Skan				     bool is_declaration)
10144132718Skan{
10145132718Skan  enum tag_types tag_type;
10146132718Skan  tree identifier;
10147132718Skan  tree type = NULL_TREE;
10148132718Skan  tree attributes = NULL_TREE;
10149132718Skan
10150132718Skan  /* See if we're looking at the `enum' keyword.  */
10151132718Skan  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
10152132718Skan    {
10153132718Skan      /* Consume the `enum' token.  */
10154132718Skan      cp_lexer_consume_token (parser->lexer);
10155132718Skan      /* Remember that it's an enumeration type.  */
10156132718Skan      tag_type = enum_type;
10157132718Skan      /* Parse the attributes.  */
10158132718Skan      attributes = cp_parser_attributes_opt (parser);
10159132718Skan    }
10160132718Skan  /* Or, it might be `typename'.  */
10161132718Skan  else if (cp_lexer_next_token_is_keyword (parser->lexer,
10162132718Skan					   RID_TYPENAME))
10163132718Skan    {
10164132718Skan      /* Consume the `typename' token.  */
10165132718Skan      cp_lexer_consume_token (parser->lexer);
10166132718Skan      /* Remember that it's a `typename' type.  */
10167132718Skan      tag_type = typename_type;
10168132718Skan      /* The `typename' keyword is only allowed in templates.  */
10169132718Skan      if (!processing_template_decl)
10170169689Skan	pedwarn ("using %<typename%> outside of template");
10171132718Skan    }
10172132718Skan  /* Otherwise it must be a class-key.  */
10173132718Skan  else
10174132718Skan    {
10175132718Skan      tag_type = cp_parser_class_key (parser);
10176132718Skan      if (tag_type == none_type)
10177132718Skan	return error_mark_node;
10178132718Skan      /* Parse the attributes.  */
10179132718Skan      attributes = cp_parser_attributes_opt (parser);
10180132718Skan    }
10181132718Skan
10182132718Skan  /* Look for the `::' operator.  */
10183169689Skan  cp_parser_global_scope_opt (parser,
10184132718Skan			      /*current_scope_valid_p=*/false);
10185132718Skan  /* Look for the nested-name-specifier.  */
10186132718Skan  if (tag_type == typename_type)
10187132718Skan    {
10188169689Skan      if (!cp_parser_nested_name_specifier (parser,
10189132718Skan					   /*typename_keyword_p=*/true,
10190132718Skan					   /*check_dependency_p=*/true,
10191132718Skan					   /*type_p=*/true,
10192169689Skan					    is_declaration))
10193132718Skan	return error_mark_node;
10194132718Skan    }
10195132718Skan  else
10196132718Skan    /* Even though `typename' is not present, the proposed resolution
10197132718Skan       to Core Issue 180 says that in `class A<T>::B', `B' should be
10198132718Skan       considered a type-name, even if `A<T>' is dependent.  */
10199132718Skan    cp_parser_nested_name_specifier_opt (parser,
10200132718Skan					 /*typename_keyword_p=*/true,
10201132718Skan					 /*check_dependency_p=*/true,
10202132718Skan					 /*type_p=*/true,
10203132718Skan					 is_declaration);
10204169689Skan  /* For everything but enumeration types, consider a template-id.
10205169689Skan     For an enumeration type, consider only a plain identifier.  */
10206132718Skan  if (tag_type != enum_type)
10207132718Skan    {
10208132718Skan      bool template_p = false;
10209132718Skan      tree decl;
10210132718Skan
10211132718Skan      /* Allow the `template' keyword.  */
10212132718Skan      template_p = cp_parser_optional_template_keyword (parser);
10213132718Skan      /* If we didn't see `template', we don't know if there's a
10214169689Skan	 template-id or not.  */
10215132718Skan      if (!template_p)
10216132718Skan	cp_parser_parse_tentatively (parser);
10217132718Skan      /* Parse the template-id.  */
10218132718Skan      decl = cp_parser_template_id (parser, template_p,
10219132718Skan				    /*check_dependency_p=*/true,
10220132718Skan				    is_declaration);
10221132718Skan      /* If we didn't find a template-id, look for an ordinary
10222169689Skan	 identifier.  */
10223132718Skan      if (!template_p && !cp_parser_parse_definitely (parser))
10224132718Skan	;
10225132718Skan      /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
10226132718Skan	 in effect, then we must assume that, upon instantiation, the
10227132718Skan	 template will correspond to a class.  */
10228132718Skan      else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
10229132718Skan	       && tag_type == typename_type)
10230132718Skan	type = make_typename_type (parser->scope, decl,
10231169689Skan				   typename_type,
10232169689Skan				   /*complain=*/tf_error);
10233169689Skan      else
10234132718Skan	type = TREE_TYPE (decl);
10235132718Skan    }
10236132718Skan
10237132718Skan  if (!type)
10238132718Skan    {
10239132718Skan      identifier = cp_parser_identifier (parser);
10240132718Skan
10241132718Skan      if (identifier == error_mark_node)
10242132718Skan	{
10243132718Skan	  parser->scope = NULL_TREE;
10244132718Skan	  return error_mark_node;
10245132718Skan	}
10246132718Skan
10247132718Skan      /* For a `typename', we needn't call xref_tag.  */
10248161651Skan      if (tag_type == typename_type
10249161651Skan	  && TREE_CODE (parser->scope) != NAMESPACE_DECL)
10250169689Skan	return cp_parser_make_typename_type (parser, parser->scope,
10251169689Skan					     identifier);
10252132718Skan      /* Look up a qualified name in the usual way.  */
10253132718Skan      if (parser->scope)
10254132718Skan	{
10255132718Skan	  tree decl;
10256132718Skan
10257169689Skan	  decl = cp_parser_lookup_name (parser, identifier,
10258169689Skan					tag_type,
10259132718Skan					/*is_template=*/false,
10260132718Skan					/*is_namespace=*/false,
10261169689Skan					/*check_dependency=*/true,
10262169689Skan					/*ambiguous_decls=*/NULL);
10263132718Skan
10264132718Skan	  /* If we are parsing friend declaration, DECL may be a
10265132718Skan	     TEMPLATE_DECL tree node here.  However, we need to check
10266132718Skan	     whether this TEMPLATE_DECL results in valid code.  Consider
10267132718Skan	     the following example:
10268132718Skan
10269132718Skan	       namespace N {
10270132718Skan		 template <class T> class C {};
10271132718Skan	       }
10272132718Skan	       class X {
10273132718Skan		 template <class T> friend class N::C; // #1, valid code
10274132718Skan	       };
10275132718Skan	       template <class T> class Y {
10276132718Skan		 friend class N::C;		       // #2, invalid code
10277132718Skan	       };
10278132718Skan
10279132718Skan	     For both case #1 and #2, we arrive at a TEMPLATE_DECL after
10280132718Skan	     name lookup of `N::C'.  We see that friend declaration must
10281132718Skan	     be template for the code to be valid.  Note that
10282132718Skan	     processing_template_decl does not work here since it is
10283132718Skan	     always 1 for the above two cases.  */
10284132718Skan
10285169689Skan	  decl = (cp_parser_maybe_treat_template_as_class
10286132718Skan		  (decl, /*tag_name_p=*/is_friend
10287132718Skan			 && parser->num_template_parameter_lists));
10288132718Skan
10289132718Skan	  if (TREE_CODE (decl) != TYPE_DECL)
10290132718Skan	    {
10291169689Skan	      cp_parser_diagnose_invalid_type_name (parser,
10292169689Skan						    parser->scope,
10293169689Skan						    identifier);
10294132718Skan	      return error_mark_node;
10295132718Skan	    }
10296132718Skan
10297132718Skan	  if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
10298169689Skan            {
10299169689Skan              bool allow_template = (parser->num_template_parameter_lists
10300169689Skan		                      || DECL_SELF_REFERENCE_P (decl));
10301169689Skan              type = check_elaborated_type_specifier (tag_type, decl,
10302169689Skan                                                      allow_template);
10303132718Skan
10304169689Skan              if (type == error_mark_node)
10305169689Skan                return error_mark_node;
10306169689Skan            }
10307169689Skan
10308132718Skan	  type = TREE_TYPE (decl);
10309132718Skan	}
10310169689Skan      else
10311132718Skan	{
10312132718Skan	  /* An elaborated-type-specifier sometimes introduces a new type and
10313132718Skan	     sometimes names an existing type.  Normally, the rule is that it
10314132718Skan	     introduces a new type only if there is not an existing type of
10315132718Skan	     the same name already in scope.  For example, given:
10316132718Skan
10317132718Skan	       struct S {};
10318132718Skan	       void f() { struct S s; }
10319132718Skan
10320132718Skan	     the `struct S' in the body of `f' is the same `struct S' as in
10321132718Skan	     the global scope; the existing definition is used.  However, if
10322169689Skan	     there were no global declaration, this would introduce a new
10323132718Skan	     local class named `S'.
10324132718Skan
10325132718Skan	     An exception to this rule applies to the following code:
10326132718Skan
10327132718Skan	       namespace N { struct S; }
10328132718Skan
10329132718Skan	     Here, the elaborated-type-specifier names a new type
10330132718Skan	     unconditionally; even if there is already an `S' in the
10331132718Skan	     containing scope this declaration names a new type.
10332132718Skan	     This exception only applies if the elaborated-type-specifier
10333132718Skan	     forms the complete declaration:
10334132718Skan
10335169689Skan	       [class.name]
10336132718Skan
10337132718Skan	       A declaration consisting solely of `class-key identifier ;' is
10338132718Skan	       either a redeclaration of the name in the current scope or a
10339132718Skan	       forward declaration of the identifier as a class name.  It
10340132718Skan	       introduces the name into the current scope.
10341132718Skan
10342132718Skan	     We are in this situation precisely when the next token is a `;'.
10343132718Skan
10344132718Skan	     An exception to the exception is that a `friend' declaration does
10345132718Skan	     *not* name a new type; i.e., given:
10346132718Skan
10347132718Skan	       struct S { friend struct T; };
10348132718Skan
10349169689Skan	     `T' is not a new type in the scope of `S'.
10350132718Skan
10351132718Skan	     Also, `new struct S' or `sizeof (struct S)' never results in the
10352132718Skan	     definition of a new type; a new type can only be declared in a
10353132718Skan	     declaration context.  */
10354132718Skan
10355169689Skan	  tag_scope ts;
10356169689Skan	  bool template_p;
10357132718Skan
10358169689Skan	  if (is_friend)
10359169689Skan	    /* Friends have special name lookup rules.  */
10360169689Skan	    ts = ts_within_enclosing_non_class;
10361169689Skan	  else if (is_declaration
10362169689Skan		   && cp_lexer_next_token_is (parser->lexer,
10363169689Skan					      CPP_SEMICOLON))
10364169689Skan	    /* This is a `class-key identifier ;' */
10365169689Skan	    ts = ts_current;
10366169689Skan	  else
10367169689Skan	    ts = ts_global;
10368169689Skan
10369169689Skan	  template_p =
10370169689Skan	    (parser->num_template_parameter_lists
10371169689Skan	     && (cp_parser_next_token_starts_class_definition_p (parser)
10372169689Skan		 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
10373169689Skan	  /* An unqualified name was used to reference this type, so
10374169689Skan	     there were no qualifying templates.  */
10375169689Skan	  if (!cp_parser_check_template_parameters (parser,
10376169689Skan						    /*num_templates=*/0))
10377169689Skan	    return error_mark_node;
10378169689Skan	  type = xref_tag (tag_type, identifier, ts, template_p);
10379132718Skan	}
10380132718Skan    }
10381169689Skan
10382169689Skan  if (type == error_mark_node)
10383169689Skan    return error_mark_node;
10384169689Skan
10385169689Skan  /* Allow attributes on forward declarations of classes.  */
10386169689Skan  if (attributes)
10387169689Skan    {
10388169689Skan      if (TREE_CODE (type) == TYPENAME_TYPE)
10389169689Skan	warning (OPT_Wattributes,
10390169689Skan		 "attributes ignored on uninstantiated type");
10391169689Skan      else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
10392169689Skan	       && ! processing_explicit_instantiation)
10393169689Skan	warning (OPT_Wattributes,
10394169689Skan		 "attributes ignored on template instantiation");
10395169689Skan      else if (is_declaration && cp_parser_declares_only_class_p (parser))
10396169689Skan	cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
10397169689Skan      else
10398169689Skan	warning (OPT_Wattributes,
10399169689Skan		 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
10400169689Skan    }
10401169689Skan
10402132718Skan  if (tag_type != enum_type)
10403132718Skan    cp_parser_check_class_key (tag_type, type);
10404132718Skan
10405132718Skan  /* A "<" cannot follow an elaborated type specifier.  If that
10406132718Skan     happens, the user was probably trying to form a template-id.  */
10407132718Skan  cp_parser_check_for_invalid_template_id (parser, type);
10408132718Skan
10409132718Skan  return type;
10410132718Skan}
10411132718Skan
10412132718Skan/* Parse an enum-specifier.
10413132718Skan
10414132718Skan   enum-specifier:
10415132718Skan     enum identifier [opt] { enumerator-list [opt] }
10416132718Skan
10417169689Skan   GNU Extensions:
10418169689Skan     enum attributes[opt] identifier [opt] { enumerator-list [opt] }
10419169689Skan       attributes[opt]
10420132718Skan
10421169689Skan   Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
10422169689Skan   if the token stream isn't an enum-specifier after all.  */
10423169689Skan
10424132718Skanstatic tree
10425132718Skancp_parser_enum_specifier (cp_parser* parser)
10426132718Skan{
10427169689Skan  tree identifier;
10428132718Skan  tree type;
10429169689Skan  tree attributes;
10430132718Skan
10431169689Skan  /* Parse tentatively so that we can back up if we don't find a
10432169689Skan     enum-specifier.  */
10433169689Skan  cp_parser_parse_tentatively (parser);
10434132718Skan
10435169689Skan  /* Caller guarantees that the current token is 'enum', an identifier
10436169689Skan     possibly follows, and the token after that is an opening brace.
10437169689Skan     If we don't have an identifier, fabricate an anonymous name for
10438169689Skan     the enumeration being defined.  */
10439169689Skan  cp_lexer_consume_token (parser->lexer);
10440169689Skan
10441169689Skan  attributes = cp_parser_attributes_opt (parser);
10442169689Skan
10443169689Skan  if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10444132718Skan    identifier = cp_parser_identifier (parser);
10445169689Skan  else
10446169689Skan    identifier = make_anon_name ();
10447132718Skan
10448169689Skan  /* Look for the `{' but don't consume it yet.  */
10449169689Skan  if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10450169689Skan    cp_parser_simulate_error (parser);
10451132718Skan
10452169689Skan  if (!cp_parser_parse_definitely (parser))
10453169689Skan    return NULL_TREE;
10454132718Skan
10455132718Skan  /* Issue an error message if type-definitions are forbidden here.  */
10456169689Skan  if (!cp_parser_check_type_definition (parser))
10457169689Skan    type = error_mark_node;
10458169689Skan  else
10459169689Skan    /* Create the new type.  We do this before consuming the opening
10460169689Skan       brace so the enum will be recorded as being on the line of its
10461169689Skan       tag (or the 'enum' keyword, if there is no tag).  */
10462169689Skan    type = start_enum (identifier);
10463169689Skan
10464169689Skan  /* Consume the opening brace.  */
10465169689Skan  cp_lexer_consume_token (parser->lexer);
10466132718Skan
10467169689Skan  if (type == error_mark_node)
10468169689Skan    {
10469169689Skan      cp_parser_skip_to_end_of_block_or_statement (parser);
10470169689Skan      return error_mark_node;
10471169689Skan    }
10472132718Skan
10473169689Skan  /* If the next token is not '}', then there are some enumerators.  */
10474169689Skan  if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
10475132718Skan    cp_parser_enumerator_list (parser, type);
10476169689Skan
10477169689Skan  /* Consume the final '}'.  */
10478132718Skan  cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10479132718Skan
10480169689Skan  /* Look for trailing attributes to apply to this enumeration, and
10481169689Skan     apply them if appropriate.  */
10482169689Skan  if (cp_parser_allow_gnu_extensions_p (parser))
10483169689Skan    {
10484169689Skan      tree trailing_attr = cp_parser_attributes_opt (parser);
10485169689Skan      cplus_decl_attributes (&type,
10486169689Skan			     trailing_attr,
10487169689Skan			     (int) ATTR_FLAG_TYPE_IN_PLACE);
10488169689Skan    }
10489169689Skan
10490132718Skan  /* Finish up the enumeration.  */
10491132718Skan  finish_enum (type);
10492132718Skan
10493132718Skan  return type;
10494132718Skan}
10495132718Skan
10496132718Skan/* Parse an enumerator-list.  The enumerators all have the indicated
10497169689Skan   TYPE.
10498132718Skan
10499132718Skan   enumerator-list:
10500132718Skan     enumerator-definition
10501132718Skan     enumerator-list , enumerator-definition  */
10502132718Skan
10503132718Skanstatic void
10504132718Skancp_parser_enumerator_list (cp_parser* parser, tree type)
10505132718Skan{
10506132718Skan  while (true)
10507132718Skan    {
10508132718Skan      /* Parse an enumerator-definition.  */
10509132718Skan      cp_parser_enumerator_definition (parser, type);
10510169689Skan
10511169689Skan      /* If the next token is not a ',', we've reached the end of
10512169689Skan	 the list.  */
10513169689Skan      if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10514132718Skan	break;
10515132718Skan      /* Otherwise, consume the `,' and keep going.  */
10516132718Skan      cp_lexer_consume_token (parser->lexer);
10517132718Skan      /* If the next token is a `}', there is a trailing comma.  */
10518132718Skan      if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
10519132718Skan	{
10520132718Skan	  if (pedantic && !in_system_header)
10521132718Skan	    pedwarn ("comma at end of enumerator list");
10522132718Skan	  break;
10523132718Skan	}
10524132718Skan    }
10525132718Skan}
10526132718Skan
10527132718Skan/* Parse an enumerator-definition.  The enumerator has the indicated
10528132718Skan   TYPE.
10529132718Skan
10530132718Skan   enumerator-definition:
10531132718Skan     enumerator
10532132718Skan     enumerator = constant-expression
10533169689Skan
10534132718Skan   enumerator:
10535132718Skan     identifier  */
10536132718Skan
10537132718Skanstatic void
10538132718Skancp_parser_enumerator_definition (cp_parser* parser, tree type)
10539132718Skan{
10540132718Skan  tree identifier;
10541132718Skan  tree value;
10542132718Skan
10543132718Skan  /* Look for the identifier.  */
10544132718Skan  identifier = cp_parser_identifier (parser);
10545132718Skan  if (identifier == error_mark_node)
10546132718Skan    return;
10547169689Skan
10548169689Skan  /* If the next token is an '=', then there is an explicit value.  */
10549169689Skan  if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10550132718Skan    {
10551132718Skan      /* Consume the `=' token.  */
10552132718Skan      cp_lexer_consume_token (parser->lexer);
10553132718Skan      /* Parse the value.  */
10554169689Skan      value = cp_parser_constant_expression (parser,
10555132718Skan					     /*allow_non_constant_p=*/false,
10556132718Skan					     NULL);
10557132718Skan    }
10558132718Skan  else
10559132718Skan    value = NULL_TREE;
10560132718Skan
10561132718Skan  /* Create the enumerator.  */
10562132718Skan  build_enumerator (identifier, value, type);
10563132718Skan}
10564132718Skan
10565132718Skan/* Parse a namespace-name.
10566132718Skan
10567132718Skan   namespace-name:
10568132718Skan     original-namespace-name
10569132718Skan     namespace-alias
10570132718Skan
10571132718Skan   Returns the NAMESPACE_DECL for the namespace.  */
10572132718Skan
10573132718Skanstatic tree
10574132718Skancp_parser_namespace_name (cp_parser* parser)
10575132718Skan{
10576132718Skan  tree identifier;
10577132718Skan  tree namespace_decl;
10578132718Skan
10579132718Skan  /* Get the name of the namespace.  */
10580132718Skan  identifier = cp_parser_identifier (parser);
10581132718Skan  if (identifier == error_mark_node)
10582132718Skan    return error_mark_node;
10583132718Skan
10584132718Skan  /* Look up the identifier in the currently active scope.  Look only
10585132718Skan     for namespaces, due to:
10586132718Skan
10587132718Skan       [basic.lookup.udir]
10588132718Skan
10589132718Skan       When looking up a namespace-name in a using-directive or alias
10590169689Skan       definition, only namespace names are considered.
10591132718Skan
10592132718Skan     And:
10593132718Skan
10594132718Skan       [basic.lookup.qual]
10595132718Skan
10596132718Skan       During the lookup of a name preceding the :: scope resolution
10597169689Skan       operator, object, function, and enumerator names are ignored.
10598132718Skan
10599132718Skan     (Note that cp_parser_class_or_namespace_name only calls this
10600132718Skan     function if the token after the name is the scope resolution
10601132718Skan     operator.)  */
10602132718Skan  namespace_decl = cp_parser_lookup_name (parser, identifier,
10603169689Skan					  none_type,
10604132718Skan					  /*is_template=*/false,
10605132718Skan					  /*is_namespace=*/true,
10606169689Skan					  /*check_dependency=*/true,
10607169689Skan					  /*ambiguous_decls=*/NULL);
10608132718Skan  /* If it's not a namespace, issue an error.  */
10609132718Skan  if (namespace_decl == error_mark_node
10610132718Skan      || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
10611132718Skan    {
10612169689Skan      if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
10613169689Skan	error ("%qD is not a namespace-name", identifier);
10614132718Skan      cp_parser_error (parser, "expected namespace-name");
10615132718Skan      namespace_decl = error_mark_node;
10616132718Skan    }
10617169689Skan
10618132718Skan  return namespace_decl;
10619132718Skan}
10620132718Skan
10621132718Skan/* Parse a namespace-definition.
10622132718Skan
10623132718Skan   namespace-definition:
10624132718Skan     named-namespace-definition
10625169689Skan     unnamed-namespace-definition
10626132718Skan
10627132718Skan   named-namespace-definition:
10628132718Skan     original-namespace-definition
10629132718Skan     extension-namespace-definition
10630132718Skan
10631132718Skan   original-namespace-definition:
10632132718Skan     namespace identifier { namespace-body }
10633169689Skan
10634132718Skan   extension-namespace-definition:
10635132718Skan     namespace original-namespace-name { namespace-body }
10636169689Skan
10637132718Skan   unnamed-namespace-definition:
10638132718Skan     namespace { namespace-body } */
10639132718Skan
10640132718Skanstatic void
10641132718Skancp_parser_namespace_definition (cp_parser* parser)
10642132718Skan{
10643169689Skan  tree identifier, attribs;
10644132718Skan
10645132718Skan  /* Look for the `namespace' keyword.  */
10646132718Skan  cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10647132718Skan
10648132718Skan  /* Get the name of the namespace.  We do not attempt to distinguish
10649132718Skan     between an original-namespace-definition and an
10650132718Skan     extension-namespace-definition at this point.  The semantic
10651132718Skan     analysis routines are responsible for that.  */
10652132718Skan  if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10653132718Skan    identifier = cp_parser_identifier (parser);
10654132718Skan  else
10655132718Skan    identifier = NULL_TREE;
10656132718Skan
10657169689Skan  /* Parse any specified attributes.  */
10658169689Skan  attribs = cp_parser_attributes_opt (parser);
10659169689Skan
10660132718Skan  /* Look for the `{' to start the namespace.  */
10661132718Skan  cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
10662132718Skan  /* Start the namespace.  */
10663169689Skan  push_namespace_with_attribs (identifier, attribs);
10664132718Skan  /* Parse the body of the namespace.  */
10665132718Skan  cp_parser_namespace_body (parser);
10666132718Skan  /* Finish the namespace.  */
10667132718Skan  pop_namespace ();
10668132718Skan  /* Look for the final `}'.  */
10669132718Skan  cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10670132718Skan}
10671132718Skan
10672132718Skan/* Parse a namespace-body.
10673132718Skan
10674132718Skan   namespace-body:
10675132718Skan     declaration-seq [opt]  */
10676132718Skan
10677132718Skanstatic void
10678132718Skancp_parser_namespace_body (cp_parser* parser)
10679132718Skan{
10680132718Skan  cp_parser_declaration_seq_opt (parser);
10681132718Skan}
10682132718Skan
10683132718Skan/* Parse a namespace-alias-definition.
10684132718Skan
10685132718Skan   namespace-alias-definition:
10686132718Skan     namespace identifier = qualified-namespace-specifier ;  */
10687132718Skan
10688132718Skanstatic void
10689132718Skancp_parser_namespace_alias_definition (cp_parser* parser)
10690132718Skan{
10691132718Skan  tree identifier;
10692132718Skan  tree namespace_specifier;
10693132718Skan
10694132718Skan  /* Look for the `namespace' keyword.  */
10695132718Skan  cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10696132718Skan  /* Look for the identifier.  */
10697132718Skan  identifier = cp_parser_identifier (parser);
10698132718Skan  if (identifier == error_mark_node)
10699132718Skan    return;
10700132718Skan  /* Look for the `=' token.  */
10701132718Skan  cp_parser_require (parser, CPP_EQ, "`='");
10702132718Skan  /* Look for the qualified-namespace-specifier.  */
10703169689Skan  namespace_specifier
10704132718Skan    = cp_parser_qualified_namespace_specifier (parser);
10705132718Skan  /* Look for the `;' token.  */
10706132718Skan  cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10707132718Skan
10708132718Skan  /* Register the alias in the symbol table.  */
10709132718Skan  do_namespace_alias (identifier, namespace_specifier);
10710132718Skan}
10711132718Skan
10712132718Skan/* Parse a qualified-namespace-specifier.
10713132718Skan
10714132718Skan   qualified-namespace-specifier:
10715132718Skan     :: [opt] nested-name-specifier [opt] namespace-name
10716132718Skan
10717132718Skan   Returns a NAMESPACE_DECL corresponding to the specified
10718132718Skan   namespace.  */
10719132718Skan
10720132718Skanstatic tree
10721132718Skancp_parser_qualified_namespace_specifier (cp_parser* parser)
10722132718Skan{
10723132718Skan  /* Look for the optional `::'.  */
10724169689Skan  cp_parser_global_scope_opt (parser,
10725132718Skan			      /*current_scope_valid_p=*/false);
10726132718Skan
10727132718Skan  /* Look for the optional nested-name-specifier.  */
10728132718Skan  cp_parser_nested_name_specifier_opt (parser,
10729132718Skan				       /*typename_keyword_p=*/false,
10730132718Skan				       /*check_dependency_p=*/true,
10731132718Skan				       /*type_p=*/false,
10732132718Skan				       /*is_declaration=*/true);
10733132718Skan
10734132718Skan  return cp_parser_namespace_name (parser);
10735132718Skan}
10736132718Skan
10737169689Skan/* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
10738169689Skan   access declaration.
10739132718Skan
10740132718Skan   using-declaration:
10741132718Skan     using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
10742169689Skan     using :: unqualified-id ;
10743132718Skan
10744169689Skan   access-declaration:
10745169689Skan     qualified-id ;
10746169689Skan
10747169689Skan   */
10748169689Skan
10749169689Skanstatic bool
10750169689Skancp_parser_using_declaration (cp_parser* parser,
10751169689Skan			     bool access_declaration_p)
10752132718Skan{
10753132718Skan  cp_token *token;
10754132718Skan  bool typename_p = false;
10755132718Skan  bool global_scope_p;
10756132718Skan  tree decl;
10757132718Skan  tree identifier;
10758132718Skan  tree qscope;
10759132718Skan
10760169689Skan  if (access_declaration_p)
10761169689Skan    cp_parser_parse_tentatively (parser);
10762169689Skan  else
10763132718Skan    {
10764169689Skan      /* Look for the `using' keyword.  */
10765169689Skan      cp_parser_require_keyword (parser, RID_USING, "`using'");
10766169689Skan
10767169689Skan      /* Peek at the next token.  */
10768169689Skan      token = cp_lexer_peek_token (parser->lexer);
10769169689Skan      /* See if it's `typename'.  */
10770169689Skan      if (token->keyword == RID_TYPENAME)
10771169689Skan	{
10772169689Skan	  /* Remember that we've seen it.  */
10773169689Skan	  typename_p = true;
10774169689Skan	  /* Consume the `typename' token.  */
10775169689Skan	  cp_lexer_consume_token (parser->lexer);
10776169689Skan	}
10777132718Skan    }
10778132718Skan
10779132718Skan  /* Look for the optional global scope qualification.  */
10780169689Skan  global_scope_p
10781132718Skan    = (cp_parser_global_scope_opt (parser,
10782169689Skan				   /*current_scope_valid_p=*/false)
10783132718Skan       != NULL_TREE);
10784132718Skan
10785132718Skan  /* If we saw `typename', or didn't see `::', then there must be a
10786132718Skan     nested-name-specifier present.  */
10787132718Skan  if (typename_p || !global_scope_p)
10788169689Skan    qscope = cp_parser_nested_name_specifier (parser, typename_p,
10789132718Skan					      /*check_dependency_p=*/true,
10790132718Skan					      /*type_p=*/false,
10791132718Skan					      /*is_declaration=*/true);
10792132718Skan  /* Otherwise, we could be in either of the two productions.  In that
10793132718Skan     case, treat the nested-name-specifier as optional.  */
10794132718Skan  else
10795132718Skan    qscope = cp_parser_nested_name_specifier_opt (parser,
10796132718Skan						  /*typename_keyword_p=*/false,
10797132718Skan						  /*check_dependency_p=*/true,
10798132718Skan						  /*type_p=*/false,
10799132718Skan						  /*is_declaration=*/true);
10800132718Skan  if (!qscope)
10801132718Skan    qscope = global_namespace;
10802132718Skan
10803169689Skan  if (access_declaration_p && cp_parser_error_occurred (parser))
10804169689Skan    /* Something has already gone wrong; there's no need to parse
10805169689Skan       further.  Since an error has occurred, the return value of
10806169689Skan       cp_parser_parse_definitely will be false, as required.  */
10807169689Skan    return cp_parser_parse_definitely (parser);
10808169689Skan
10809132718Skan  /* Parse the unqualified-id.  */
10810169689Skan  identifier = cp_parser_unqualified_id (parser,
10811132718Skan					 /*template_keyword_p=*/false,
10812132718Skan					 /*check_dependency_p=*/true,
10813169689Skan					 /*declarator_p=*/true,
10814169689Skan					 /*optional_p=*/false);
10815132718Skan
10816169689Skan  if (access_declaration_p)
10817169689Skan    {
10818169689Skan      if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10819169689Skan	cp_parser_simulate_error (parser);
10820169689Skan      if (!cp_parser_parse_definitely (parser))
10821169689Skan	return false;
10822169689Skan    }
10823169689Skan
10824132718Skan  /* The function we call to handle a using-declaration is different
10825132718Skan     depending on what scope we are in.  */
10826169689Skan  if (qscope == error_mark_node || identifier == error_mark_node)
10827132718Skan    ;
10828132718Skan  else if (TREE_CODE (identifier) != IDENTIFIER_NODE
10829132718Skan	   && TREE_CODE (identifier) != BIT_NOT_EXPR)
10830132718Skan    /* [namespace.udecl]
10831132718Skan
10832132718Skan       A using declaration shall not name a template-id.  */
10833132718Skan    error ("a template-id may not appear in a using-declaration");
10834132718Skan  else
10835132718Skan    {
10836169689Skan      if (at_class_scope_p ())
10837132718Skan	{
10838132718Skan	  /* Create the USING_DECL.  */
10839169689Skan	  decl = do_class_using_decl (parser->scope, identifier);
10840132718Skan	  /* Add it to the list of members in this class.  */
10841132718Skan	  finish_member_declaration (decl);
10842132718Skan	}
10843132718Skan      else
10844132718Skan	{
10845132718Skan	  decl = cp_parser_lookup_name_simple (parser, identifier);
10846132718Skan	  if (decl == error_mark_node)
10847132718Skan	    cp_parser_name_lookup_error (parser, identifier, decl, NULL);
10848169689Skan	  else if (!at_namespace_scope_p ())
10849132718Skan	    do_local_using_decl (decl, qscope, identifier);
10850132718Skan	  else
10851132718Skan	    do_toplevel_using_decl (decl, qscope, identifier);
10852132718Skan	}
10853132718Skan    }
10854132718Skan
10855132718Skan  /* Look for the final `;'.  */
10856132718Skan  cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10857169689Skan
10858169689Skan  return true;
10859132718Skan}
10860132718Skan
10861169689Skan/* Parse a using-directive.
10862169689Skan
10863132718Skan   using-directive:
10864132718Skan     using namespace :: [opt] nested-name-specifier [opt]
10865132718Skan       namespace-name ;  */
10866132718Skan
10867132718Skanstatic void
10868132718Skancp_parser_using_directive (cp_parser* parser)
10869132718Skan{
10870132718Skan  tree namespace_decl;
10871132718Skan  tree attribs;
10872132718Skan
10873132718Skan  /* Look for the `using' keyword.  */
10874132718Skan  cp_parser_require_keyword (parser, RID_USING, "`using'");
10875132718Skan  /* And the `namespace' keyword.  */
10876132718Skan  cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10877132718Skan  /* Look for the optional `::' operator.  */
10878132718Skan  cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
10879132718Skan  /* And the optional nested-name-specifier.  */
10880132718Skan  cp_parser_nested_name_specifier_opt (parser,
10881132718Skan				       /*typename_keyword_p=*/false,
10882132718Skan				       /*check_dependency_p=*/true,
10883132718Skan				       /*type_p=*/false,
10884132718Skan				       /*is_declaration=*/true);
10885132718Skan  /* Get the namespace being used.  */
10886132718Skan  namespace_decl = cp_parser_namespace_name (parser);
10887132718Skan  /* And any specified attributes.  */
10888132718Skan  attribs = cp_parser_attributes_opt (parser);
10889132718Skan  /* Update the symbol table.  */
10890132718Skan  parse_using_directive (namespace_decl, attribs);
10891132718Skan  /* Look for the final `;'.  */
10892132718Skan  cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10893132718Skan}
10894132718Skan
10895132718Skan/* Parse an asm-definition.
10896132718Skan
10897132718Skan   asm-definition:
10898169689Skan     asm ( string-literal ) ;
10899132718Skan
10900132718Skan   GNU Extension:
10901132718Skan
10902132718Skan   asm-definition:
10903132718Skan     asm volatile [opt] ( string-literal ) ;
10904132718Skan     asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
10905132718Skan     asm volatile [opt] ( string-literal : asm-operand-list [opt]
10906169689Skan			  : asm-operand-list [opt] ) ;
10907169689Skan     asm volatile [opt] ( string-literal : asm-operand-list [opt]
10908169689Skan			  : asm-operand-list [opt]
10909169689Skan			  : asm-operand-list [opt] ) ;  */
10910132718Skan
10911132718Skanstatic void
10912132718Skancp_parser_asm_definition (cp_parser* parser)
10913132718Skan{
10914132718Skan  tree string;
10915132718Skan  tree outputs = NULL_TREE;
10916132718Skan  tree inputs = NULL_TREE;
10917132718Skan  tree clobbers = NULL_TREE;
10918132718Skan  tree asm_stmt;
10919132718Skan  bool volatile_p = false;
10920132718Skan  bool extended_p = false;
10921132718Skan
10922132718Skan  /* Look for the `asm' keyword.  */
10923132718Skan  cp_parser_require_keyword (parser, RID_ASM, "`asm'");
10924132718Skan  /* See if the next token is `volatile'.  */
10925132718Skan  if (cp_parser_allow_gnu_extensions_p (parser)
10926132718Skan      && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
10927132718Skan    {
10928132718Skan      /* Remember that we saw the `volatile' keyword.  */
10929132718Skan      volatile_p = true;
10930132718Skan      /* Consume the token.  */
10931132718Skan      cp_lexer_consume_token (parser->lexer);
10932132718Skan    }
10933132718Skan  /* Look for the opening `('.  */
10934169689Skan  if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
10935169689Skan    return;
10936132718Skan  /* Look for the string.  */
10937169689Skan  string = cp_parser_string_literal (parser, false, false);
10938169689Skan  if (string == error_mark_node)
10939169689Skan    {
10940169689Skan      cp_parser_skip_to_closing_parenthesis (parser, true, false,
10941169689Skan					     /*consume_paren=*/true);
10942169689Skan      return;
10943169689Skan    }
10944169689Skan
10945132718Skan  /* If we're allowing GNU extensions, check for the extended assembly
10946169689Skan     syntax.  Unfortunately, the `:' tokens need not be separated by
10947132718Skan     a space in C, and so, for compatibility, we tolerate that here
10948132718Skan     too.  Doing that means that we have to treat the `::' operator as
10949132718Skan     two `:' tokens.  */
10950132718Skan  if (cp_parser_allow_gnu_extensions_p (parser)
10951169689Skan      && parser->in_function_body
10952132718Skan      && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
10953132718Skan	  || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
10954132718Skan    {
10955132718Skan      bool inputs_p = false;
10956132718Skan      bool clobbers_p = false;
10957132718Skan
10958132718Skan      /* The extended syntax was used.  */
10959132718Skan      extended_p = true;
10960132718Skan
10961132718Skan      /* Look for outputs.  */
10962132718Skan      if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10963132718Skan	{
10964132718Skan	  /* Consume the `:'.  */
10965132718Skan	  cp_lexer_consume_token (parser->lexer);
10966132718Skan	  /* Parse the output-operands.  */
10967169689Skan	  if (cp_lexer_next_token_is_not (parser->lexer,
10968132718Skan					  CPP_COLON)
10969132718Skan	      && cp_lexer_next_token_is_not (parser->lexer,
10970132718Skan					     CPP_SCOPE)
10971132718Skan	      && cp_lexer_next_token_is_not (parser->lexer,
10972132718Skan					     CPP_CLOSE_PAREN))
10973132718Skan	    outputs = cp_parser_asm_operand_list (parser);
10974132718Skan	}
10975132718Skan      /* If the next token is `::', there are no outputs, and the
10976132718Skan	 next token is the beginning of the inputs.  */
10977132718Skan      else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10978146895Skan	/* The inputs are coming next.  */
10979146895Skan	inputs_p = true;
10980132718Skan
10981132718Skan      /* Look for inputs.  */
10982132718Skan      if (inputs_p
10983132718Skan	  || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10984132718Skan	{
10985146895Skan	  /* Consume the `:' or `::'.  */
10986146895Skan	  cp_lexer_consume_token (parser->lexer);
10987132718Skan	  /* Parse the output-operands.  */
10988169689Skan	  if (cp_lexer_next_token_is_not (parser->lexer,
10989132718Skan					  CPP_COLON)
10990132718Skan	      && cp_lexer_next_token_is_not (parser->lexer,
10991132718Skan					     CPP_CLOSE_PAREN))
10992132718Skan	    inputs = cp_parser_asm_operand_list (parser);
10993132718Skan	}
10994132718Skan      else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10995132718Skan	/* The clobbers are coming next.  */
10996132718Skan	clobbers_p = true;
10997132718Skan
10998132718Skan      /* Look for clobbers.  */
10999169689Skan      if (clobbers_p
11000132718Skan	  || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11001132718Skan	{
11002146895Skan	  /* Consume the `:' or `::'.  */
11003146895Skan	  cp_lexer_consume_token (parser->lexer);
11004132718Skan	  /* Parse the clobbers.  */
11005132718Skan	  if (cp_lexer_next_token_is_not (parser->lexer,
11006132718Skan					  CPP_CLOSE_PAREN))
11007132718Skan	    clobbers = cp_parser_asm_clobber_list (parser);
11008132718Skan	}
11009132718Skan    }
11010132718Skan  /* Look for the closing `)'.  */
11011132718Skan  if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
11012132718Skan    cp_parser_skip_to_closing_parenthesis (parser, true, false,
11013132718Skan					   /*consume_paren=*/true);
11014132718Skan  cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11015132718Skan
11016169689Skan  /* Create the ASM_EXPR.  */
11017169689Skan  if (parser->in_function_body)
11018132718Skan    {
11019169689Skan      asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
11020169689Skan				  inputs, clobbers);
11021169689Skan      /* If the extended syntax was not used, mark the ASM_EXPR.  */
11022132718Skan      if (!extended_p)
11023169689Skan	{
11024169689Skan	  tree temp = asm_stmt;
11025169689Skan	  if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
11026169689Skan	    temp = TREE_OPERAND (temp, 0);
11027169689Skan
11028169689Skan	  ASM_INPUT_P (temp) = 1;
11029169689Skan	}
11030132718Skan    }
11031132718Skan  else
11032169689Skan    cgraph_add_asm_node (string);
11033132718Skan}
11034132718Skan
11035132718Skan/* Declarators [gram.dcl.decl] */
11036132718Skan
11037132718Skan/* Parse an init-declarator.
11038132718Skan
11039132718Skan   init-declarator:
11040132718Skan     declarator initializer [opt]
11041132718Skan
11042132718Skan   GNU Extension:
11043132718Skan
11044132718Skan   init-declarator:
11045132718Skan     declarator asm-specification [opt] attributes [opt] initializer [opt]
11046132718Skan
11047132718Skan   function-definition:
11048132718Skan     decl-specifier-seq [opt] declarator ctor-initializer [opt]
11049169689Skan       function-body
11050169689Skan     decl-specifier-seq [opt] declarator function-try-block
11051132718Skan
11052132718Skan   GNU Extension:
11053132718Skan
11054132718Skan   function-definition:
11055169689Skan     __extension__ function-definition
11056132718Skan
11057169689Skan   The DECL_SPECIFIERS apply to this declarator.  Returns a
11058169689Skan   representation of the entity declared.  If MEMBER_P is TRUE, then
11059169689Skan   this declarator appears in a class scope.  The new DECL created by
11060169689Skan   this declarator is returned.
11061132718Skan
11062169689Skan   The CHECKS are access checks that should be performed once we know
11063169689Skan   what entity is being declared (and, therefore, what classes have
11064169689Skan   befriended it).
11065169689Skan
11066132718Skan   If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
11067132718Skan   for a function-definition here as well.  If the declarator is a
11068132718Skan   declarator for a function-definition, *FUNCTION_DEFINITION_P will
11069132718Skan   be TRUE upon return.  By that point, the function-definition will
11070132718Skan   have been completely parsed.
11071132718Skan
11072132718Skan   FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
11073132718Skan   is FALSE.  */
11074132718Skan
11075132718Skanstatic tree
11076169689Skancp_parser_init_declarator (cp_parser* parser,
11077169689Skan			   cp_decl_specifier_seq *decl_specifiers,
11078169689Skan			   VEC (deferred_access_check,gc)* checks,
11079132718Skan			   bool function_definition_allowed_p,
11080132718Skan			   bool member_p,
11081132718Skan			   int declares_class_or_enum,
11082132718Skan			   bool* function_definition_p)
11083132718Skan{
11084132718Skan  cp_token *token;
11085169689Skan  cp_declarator *declarator;
11086169689Skan  tree prefix_attributes;
11087132718Skan  tree attributes;
11088132718Skan  tree asm_specification;
11089132718Skan  tree initializer;
11090132718Skan  tree decl = NULL_TREE;
11091132718Skan  tree scope;
11092132718Skan  bool is_initialized;
11093169689Skan  /* Only valid if IS_INITIALIZED is true.  In that case, CPP_EQ if
11094169689Skan     initialized with "= ..", CPP_OPEN_PAREN if initialized with
11095169689Skan     "(...)".  */
11096169689Skan  enum cpp_ttype initialization_kind;
11097169689Skan  bool is_parenthesized_init = false;
11098132718Skan  bool is_non_constant_init;
11099132718Skan  int ctor_dtor_or_conv_p;
11100132718Skan  bool friend_p;
11101169689Skan  tree pushed_scope = NULL;
11102132718Skan
11103169689Skan  /* Gather the attributes that were provided with the
11104169689Skan     decl-specifiers.  */
11105169689Skan  prefix_attributes = decl_specifiers->attributes;
11106169689Skan
11107132718Skan  /* Assume that this is not the declarator for a function
11108132718Skan     definition.  */
11109132718Skan  if (function_definition_p)
11110132718Skan    *function_definition_p = false;
11111132718Skan
11112132718Skan  /* Defer access checks while parsing the declarator; we cannot know
11113169689Skan     what names are accessible until we know what is being
11114132718Skan     declared.  */
11115132718Skan  resume_deferring_access_checks ();
11116132718Skan
11117132718Skan  /* Parse the declarator.  */
11118169689Skan  declarator
11119132718Skan    = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11120132718Skan			    &ctor_dtor_or_conv_p,
11121146895Skan			    /*parenthesized_p=*/NULL,
11122146895Skan			    /*member_p=*/false);
11123132718Skan  /* Gather up the deferred checks.  */
11124132718Skan  stop_deferring_access_checks ();
11125132718Skan
11126132718Skan  /* If the DECLARATOR was erroneous, there's no need to go
11127132718Skan     further.  */
11128169689Skan  if (declarator == cp_error_declarator)
11129132718Skan    return error_mark_node;
11130132718Skan
11131169689Skan  /* Check that the number of template-parameter-lists is OK.  */
11132169689Skan  if (!cp_parser_check_declarator_template_parameters (parser, declarator))
11133169689Skan    return error_mark_node;
11134169689Skan
11135161651Skan  if (declares_class_or_enum & 2)
11136169689Skan    cp_parser_check_for_definition_in_return_type (declarator,
11137169689Skan						   decl_specifiers->type);
11138132718Skan
11139132718Skan  /* Figure out what scope the entity declared by the DECLARATOR is
11140132718Skan     located in.  `grokdeclarator' sometimes changes the scope, so
11141132718Skan     we compute it now.  */
11142132718Skan  scope = get_scope_of_declarator (declarator);
11143132718Skan
11144132718Skan  /* If we're allowing GNU extensions, look for an asm-specification
11145132718Skan     and attributes.  */
11146132718Skan  if (cp_parser_allow_gnu_extensions_p (parser))
11147132718Skan    {
11148132718Skan      /* Look for an asm-specification.  */
11149132718Skan      asm_specification = cp_parser_asm_specification_opt (parser);
11150132718Skan      /* And attributes.  */
11151132718Skan      attributes = cp_parser_attributes_opt (parser);
11152132718Skan    }
11153132718Skan  else
11154132718Skan    {
11155132718Skan      asm_specification = NULL_TREE;
11156132718Skan      attributes = NULL_TREE;
11157132718Skan    }
11158132718Skan
11159132718Skan  /* Peek at the next token.  */
11160132718Skan  token = cp_lexer_peek_token (parser->lexer);
11161132718Skan  /* Check to see if the token indicates the start of a
11162132718Skan     function-definition.  */
11163132718Skan  if (cp_parser_token_starts_function_definition_p (token))
11164132718Skan    {
11165132718Skan      if (!function_definition_allowed_p)
11166132718Skan	{
11167132718Skan	  /* If a function-definition should not appear here, issue an
11168132718Skan	     error message.  */
11169132718Skan	  cp_parser_error (parser,
11170132718Skan			   "a function-definition is not allowed here");
11171132718Skan	  return error_mark_node;
11172132718Skan	}
11173132718Skan      else
11174132718Skan	{
11175132718Skan	  /* Neither attributes nor an asm-specification are allowed
11176132718Skan	     on a function-definition.  */
11177132718Skan	  if (asm_specification)
11178132718Skan	    error ("an asm-specification is not allowed on a function-definition");
11179132718Skan	  if (attributes)
11180132718Skan	    error ("attributes are not allowed on a function-definition");
11181132718Skan	  /* This is a function-definition.  */
11182132718Skan	  *function_definition_p = true;
11183132718Skan
11184132718Skan	  /* Parse the function definition.  */
11185132718Skan	  if (member_p)
11186132718Skan	    decl = cp_parser_save_member_function_body (parser,
11187132718Skan							decl_specifiers,
11188132718Skan							declarator,
11189132718Skan							prefix_attributes);
11190132718Skan	  else
11191169689Skan	    decl
11192132718Skan	      = (cp_parser_function_definition_from_specifiers_and_declarator
11193132718Skan		 (parser, decl_specifiers, prefix_attributes, declarator));
11194132718Skan
11195132718Skan	  return decl;
11196132718Skan	}
11197132718Skan    }
11198132718Skan
11199132718Skan  /* [dcl.dcl]
11200132718Skan
11201132718Skan     Only in function declarations for constructors, destructors, and
11202169689Skan     type conversions can the decl-specifier-seq be omitted.
11203132718Skan
11204132718Skan     We explicitly postpone this check past the point where we handle
11205132718Skan     function-definitions because we tolerate function-definitions
11206132718Skan     that are missing their return types in some modes.  */
11207169689Skan  if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
11208132718Skan    {
11209169689Skan      cp_parser_error (parser,
11210132718Skan		       "expected constructor, destructor, or type conversion");
11211132718Skan      return error_mark_node;
11212132718Skan    }
11213132718Skan
11214132718Skan  /* An `=' or an `(' indicates an initializer.  */
11215169689Skan  if (token->type == CPP_EQ
11216169689Skan      || token->type == CPP_OPEN_PAREN)
11217132718Skan    {
11218169689Skan      is_initialized = true;
11219169689Skan      initialization_kind = token->type;
11220132718Skan    }
11221169689Skan  else
11222169689Skan    {
11223169689Skan      /* If the init-declarator isn't initialized and isn't followed by a
11224169689Skan	 `,' or `;', it's not a valid init-declarator.  */
11225169689Skan      if (token->type != CPP_COMMA
11226169689Skan	  && token->type != CPP_SEMICOLON)
11227169689Skan	{
11228169689Skan	  cp_parser_error (parser, "expected initializer");
11229169689Skan	  return error_mark_node;
11230169689Skan	}
11231169689Skan      is_initialized = false;
11232169689Skan      initialization_kind = CPP_EOF;
11233169689Skan    }
11234132718Skan
11235132718Skan  /* Because start_decl has side-effects, we should only call it if we
11236132718Skan     know we're going ahead.  By this point, we know that we cannot
11237132718Skan     possibly be looking at any other construct.  */
11238132718Skan  cp_parser_commit_to_tentative_parse (parser);
11239132718Skan
11240132718Skan  /* If the decl specifiers were bad, issue an error now that we're
11241132718Skan     sure this was intended to be a declarator.  Then continue
11242132718Skan     declaring the variable(s), as int, to try to cut down on further
11243132718Skan     errors.  */
11244169689Skan  if (decl_specifiers->any_specifiers_p
11245169689Skan      && decl_specifiers->type == error_mark_node)
11246132718Skan    {
11247132718Skan      cp_parser_error (parser, "invalid type in declaration");
11248169689Skan      decl_specifiers->type = integer_type_node;
11249132718Skan    }
11250132718Skan
11251132718Skan  /* Check to see whether or not this declaration is a friend.  */
11252132718Skan  friend_p = cp_parser_friend_p (decl_specifiers);
11253132718Skan
11254132718Skan  /* Enter the newly declared entry in the symbol table.  If we're
11255132718Skan     processing a declaration in a class-specifier, we wait until
11256132718Skan     after processing the initializer.  */
11257132718Skan  if (!member_p)
11258132718Skan    {
11259132718Skan      if (parser->in_unbraced_linkage_specification_p)
11260169689Skan	decl_specifiers->storage_class = sc_extern;
11261132718Skan      decl = start_decl (declarator, decl_specifiers,
11262169689Skan			 is_initialized, attributes, prefix_attributes,
11263169689Skan			 &pushed_scope);
11264132718Skan    }
11265169689Skan  else if (scope)
11266169689Skan    /* Enter the SCOPE.  That way unqualified names appearing in the
11267169689Skan       initializer will be looked up in SCOPE.  */
11268169689Skan    pushed_scope = push_scope (scope);
11269132718Skan
11270132718Skan  /* Perform deferred access control checks, now that we know in which
11271132718Skan     SCOPE the declared entity resides.  */
11272169689Skan  if (!member_p && decl)
11273132718Skan    {
11274132718Skan      tree saved_current_function_decl = NULL_TREE;
11275132718Skan
11276132718Skan      /* If the entity being declared is a function, pretend that we
11277132718Skan	 are in its scope.  If it is a `friend', it may have access to
11278132718Skan	 things that would not otherwise be accessible.  */
11279132718Skan      if (TREE_CODE (decl) == FUNCTION_DECL)
11280132718Skan	{
11281132718Skan	  saved_current_function_decl = current_function_decl;
11282132718Skan	  current_function_decl = decl;
11283132718Skan	}
11284169689Skan
11285169689Skan      /* Perform access checks for template parameters.  */
11286169689Skan      cp_parser_perform_template_parameter_access_checks (checks);
11287169689Skan
11288132718Skan      /* Perform the access control checks for the declarator and the
11289132718Skan	 the decl-specifiers.  */
11290132718Skan      perform_deferred_access_checks ();
11291132718Skan
11292132718Skan      /* Restore the saved value.  */
11293132718Skan      if (TREE_CODE (decl) == FUNCTION_DECL)
11294132718Skan	current_function_decl = saved_current_function_decl;
11295132718Skan    }
11296132718Skan
11297132718Skan  /* Parse the initializer.  */
11298169689Skan  initializer = NULL_TREE;
11299169689Skan  is_parenthesized_init = false;
11300169689Skan  is_non_constant_init = true;
11301132718Skan  if (is_initialized)
11302132718Skan    {
11303169689Skan      if (function_declarator_p (declarator))
11304169689Skan	{
11305169689Skan	   if (initialization_kind == CPP_EQ)
11306169689Skan	     initializer = cp_parser_pure_specifier (parser);
11307169689Skan	   else
11308169689Skan	     {
11309169689Skan	       /* If the declaration was erroneous, we don't really
11310169689Skan		  know what the user intended, so just silently
11311169689Skan		  consume the initializer.  */
11312169689Skan	       if (decl != error_mark_node)
11313169689Skan		 error ("initializer provided for function");
11314169689Skan	       cp_parser_skip_to_closing_parenthesis (parser,
11315169689Skan						      /*recovering=*/true,
11316169689Skan						      /*or_comma=*/false,
11317169689Skan						      /*consume_paren=*/true);
11318169689Skan	     }
11319169689Skan	}
11320169689Skan      else
11321169689Skan	initializer = cp_parser_initializer (parser,
11322169689Skan					     &is_parenthesized_init,
11323169689Skan					     &is_non_constant_init);
11324132718Skan    }
11325132718Skan
11326132718Skan  /* The old parser allows attributes to appear after a parenthesized
11327132718Skan     initializer.  Mark Mitchell proposed removing this functionality
11328132718Skan     on the GCC mailing lists on 2002-08-13.  This parser accepts the
11329132718Skan     attributes -- but ignores them.  */
11330132718Skan  if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
11331132718Skan    if (cp_parser_attributes_opt (parser))
11332169689Skan      warning (OPT_Wattributes,
11333169689Skan	       "attributes after parenthesized initializer ignored");
11334132718Skan
11335132718Skan  /* For an in-class declaration, use `grokfield' to create the
11336132718Skan     declaration.  */
11337132718Skan  if (member_p)
11338132718Skan    {
11339169689Skan      if (pushed_scope)
11340169689Skan	{
11341169689Skan	  pop_scope (pushed_scope);
11342169689Skan	  pushed_scope = false;
11343169689Skan	}
11344132718Skan      decl = grokfield (declarator, decl_specifiers,
11345169689Skan			initializer, !is_non_constant_init,
11346169689Skan			/*asmspec=*/NULL_TREE,
11347169689Skan			prefix_attributes);
11348132718Skan      if (decl && TREE_CODE (decl) == FUNCTION_DECL)
11349132718Skan	cp_parser_save_default_args (parser, decl);
11350132718Skan    }
11351169689Skan
11352132718Skan  /* Finish processing the declaration.  But, skip friend
11353132718Skan     declarations.  */
11354169689Skan  if (!friend_p && decl && decl != error_mark_node)
11355169689Skan    {
11356169689Skan      cp_finish_decl (decl,
11357169689Skan		      initializer, !is_non_constant_init,
11358169689Skan		      asm_specification,
11359169689Skan		      /* If the initializer is in parentheses, then this is
11360169689Skan			 a direct-initialization, which means that an
11361169689Skan			 `explicit' constructor is OK.  Otherwise, an
11362169689Skan			 `explicit' constructor cannot be used.  */
11363169689Skan		      ((is_parenthesized_init || !is_initialized)
11364132718Skan		     ? 0 : LOOKUP_ONLYCONVERTING));
11365169689Skan    }
11366169689Skan  if (!friend_p && pushed_scope)
11367169689Skan    pop_scope (pushed_scope);
11368132718Skan
11369132718Skan  return decl;
11370132718Skan}
11371132718Skan
11372132718Skan/* Parse a declarator.
11373169689Skan
11374132718Skan   declarator:
11375132718Skan     direct-declarator
11376169689Skan     ptr-operator declarator
11377132718Skan
11378132718Skan   abstract-declarator:
11379132718Skan     ptr-operator abstract-declarator [opt]
11380132718Skan     direct-abstract-declarator
11381132718Skan
11382132718Skan   GNU Extensions:
11383132718Skan
11384132718Skan   declarator:
11385132718Skan     attributes [opt] direct-declarator
11386169689Skan     attributes [opt] ptr-operator declarator
11387132718Skan
11388132718Skan   abstract-declarator:
11389132718Skan     attributes [opt] ptr-operator abstract-declarator [opt]
11390132718Skan     attributes [opt] direct-abstract-declarator
11391132718Skan
11392132718Skan   If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
11393132718Skan   detect constructor, destructor or conversion operators. It is set
11394132718Skan   to -1 if the declarator is a name, and +1 if it is a
11395132718Skan   function. Otherwise it is set to zero. Usually you just want to
11396132718Skan   test for >0, but internally the negative value is used.
11397169689Skan
11398132718Skan   (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
11399132718Skan   a decl-specifier-seq unless it declares a constructor, destructor,
11400132718Skan   or conversion.  It might seem that we could check this condition in
11401132718Skan   semantic analysis, rather than parsing, but that makes it difficult
11402132718Skan   to handle something like `f()'.  We want to notice that there are
11403132718Skan   no decl-specifiers, and therefore realize that this is an
11404169689Skan   expression, not a declaration.)
11405169689Skan
11406132718Skan   If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
11407146895Skan   the declarator is a direct-declarator of the form "(...)".
11408132718Skan
11409146895Skan   MEMBER_P is true iff this declarator is a member-declarator.  */
11410146895Skan
11411169689Skanstatic cp_declarator *
11412169689Skancp_parser_declarator (cp_parser* parser,
11413169689Skan		      cp_parser_declarator_kind dcl_kind,
11414169689Skan		      int* ctor_dtor_or_conv_p,
11415146895Skan		      bool* parenthesized_p,
11416146895Skan		      bool member_p)
11417132718Skan{
11418132718Skan  cp_token *token;
11419169689Skan  cp_declarator *declarator;
11420132718Skan  enum tree_code code;
11421169689Skan  cp_cv_quals cv_quals;
11422132718Skan  tree class_type;
11423132718Skan  tree attributes = NULL_TREE;
11424132718Skan
11425132718Skan  /* Assume this is not a constructor, destructor, or type-conversion
11426132718Skan     operator.  */
11427132718Skan  if (ctor_dtor_or_conv_p)
11428132718Skan    *ctor_dtor_or_conv_p = 0;
11429132718Skan
11430132718Skan  if (cp_parser_allow_gnu_extensions_p (parser))
11431132718Skan    attributes = cp_parser_attributes_opt (parser);
11432169689Skan
11433132718Skan  /* Peek at the next token.  */
11434132718Skan  token = cp_lexer_peek_token (parser->lexer);
11435169689Skan
11436132718Skan  /* Check for the ptr-operator production.  */
11437132718Skan  cp_parser_parse_tentatively (parser);
11438132718Skan  /* Parse the ptr-operator.  */
11439169689Skan  code = cp_parser_ptr_operator (parser,
11440169689Skan				 &class_type,
11441169689Skan				 &cv_quals);
11442132718Skan  /* If that worked, then we have a ptr-operator.  */
11443132718Skan  if (cp_parser_parse_definitely (parser))
11444132718Skan    {
11445132718Skan      /* If a ptr-operator was found, then this declarator was not
11446132718Skan	 parenthesized.  */
11447132718Skan      if (parenthesized_p)
11448132718Skan	*parenthesized_p = true;
11449132718Skan      /* The dependent declarator is optional if we are parsing an
11450132718Skan	 abstract-declarator.  */
11451132718Skan      if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11452132718Skan	cp_parser_parse_tentatively (parser);
11453132718Skan
11454132718Skan      /* Parse the dependent declarator.  */
11455132718Skan      declarator = cp_parser_declarator (parser, dcl_kind,
11456132718Skan					 /*ctor_dtor_or_conv_p=*/NULL,
11457146895Skan					 /*parenthesized_p=*/NULL,
11458146895Skan					 /*member_p=*/false);
11459132718Skan
11460132718Skan      /* If we are parsing an abstract-declarator, we must handle the
11461132718Skan	 case where the dependent declarator is absent.  */
11462132718Skan      if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
11463132718Skan	  && !cp_parser_parse_definitely (parser))
11464169689Skan	declarator = NULL;
11465169689Skan
11466132718Skan      /* Build the representation of the ptr-operator.  */
11467169689Skan      if (class_type)
11468169689Skan	declarator = make_ptrmem_declarator (cv_quals,
11469169689Skan					     class_type,
11470169689Skan					     declarator);
11471169689Skan      else if (code == INDIRECT_REF)
11472169689Skan	declarator = make_pointer_declarator (cv_quals, declarator);
11473132718Skan      else
11474169689Skan	declarator = make_reference_declarator (cv_quals, declarator);
11475132718Skan    }
11476132718Skan  /* Everything else is a direct-declarator.  */
11477132718Skan  else
11478132718Skan    {
11479132718Skan      if (parenthesized_p)
11480132718Skan	*parenthesized_p = cp_lexer_next_token_is (parser->lexer,
11481132718Skan						   CPP_OPEN_PAREN);
11482132718Skan      declarator = cp_parser_direct_declarator (parser, dcl_kind,
11483146895Skan						ctor_dtor_or_conv_p,
11484146895Skan						member_p);
11485132718Skan    }
11486132718Skan
11487169689Skan  if (attributes && declarator && declarator != cp_error_declarator)
11488169689Skan    declarator->attributes = attributes;
11489169689Skan
11490132718Skan  return declarator;
11491132718Skan}
11492132718Skan
11493132718Skan/* Parse a direct-declarator or direct-abstract-declarator.
11494132718Skan
11495132718Skan   direct-declarator:
11496132718Skan     declarator-id
11497132718Skan     direct-declarator ( parameter-declaration-clause )
11498169689Skan       cv-qualifier-seq [opt]
11499132718Skan       exception-specification [opt]
11500132718Skan     direct-declarator [ constant-expression [opt] ]
11501169689Skan     ( declarator )
11502132718Skan
11503132718Skan   direct-abstract-declarator:
11504132718Skan     direct-abstract-declarator [opt]
11505169689Skan       ( parameter-declaration-clause )
11506132718Skan       cv-qualifier-seq [opt]
11507132718Skan       exception-specification [opt]
11508132718Skan     direct-abstract-declarator [opt] [ constant-expression [opt] ]
11509132718Skan     ( abstract-declarator )
11510132718Skan
11511132718Skan   Returns a representation of the declarator.  DCL_KIND is
11512132718Skan   CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
11513132718Skan   direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
11514132718Skan   we are parsing a direct-declarator.  It is
11515132718Skan   CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
11516132718Skan   of ambiguity we prefer an abstract declarator, as per
11517169689Skan   [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
11518169689Skan   cp_parser_declarator.  */
11519132718Skan
11520169689Skanstatic cp_declarator *
11521132718Skancp_parser_direct_declarator (cp_parser* parser,
11522169689Skan			     cp_parser_declarator_kind dcl_kind,
11523169689Skan			     int* ctor_dtor_or_conv_p,
11524146895Skan			     bool member_p)
11525132718Skan{
11526132718Skan  cp_token *token;
11527169689Skan  cp_declarator *declarator = NULL;
11528132718Skan  tree scope = NULL_TREE;
11529132718Skan  bool saved_default_arg_ok_p = parser->default_arg_ok_p;
11530132718Skan  bool saved_in_declarator_p = parser->in_declarator_p;
11531132718Skan  bool first = true;
11532169689Skan  tree pushed_scope = NULL_TREE;
11533132718Skan
11534132718Skan  while (true)
11535132718Skan    {
11536132718Skan      /* Peek at the next token.  */
11537132718Skan      token = cp_lexer_peek_token (parser->lexer);
11538132718Skan      if (token->type == CPP_OPEN_PAREN)
11539132718Skan	{
11540132718Skan	  /* This is either a parameter-declaration-clause, or a
11541169689Skan	     parenthesized declarator. When we know we are parsing a
11542169689Skan	     named declarator, it must be a parenthesized declarator
11543169689Skan	     if FIRST is true. For instance, `(int)' is a
11544169689Skan	     parameter-declaration-clause, with an omitted
11545169689Skan	     direct-abstract-declarator. But `((*))', is a
11546169689Skan	     parenthesized abstract declarator. Finally, when T is a
11547169689Skan	     template parameter `(T)' is a
11548169689Skan	     parameter-declaration-clause, and not a parenthesized
11549169689Skan	     named declarator.
11550169689Skan
11551132718Skan	     We first try and parse a parameter-declaration-clause,
11552132718Skan	     and then try a nested declarator (if FIRST is true).
11553132718Skan
11554132718Skan	     It is not an error for it not to be a
11555132718Skan	     parameter-declaration-clause, even when FIRST is
11556132718Skan	     false. Consider,
11557132718Skan
11558132718Skan	       int i (int);
11559132718Skan	       int i (3);
11560132718Skan
11561132718Skan	     The first is the declaration of a function while the
11562132718Skan	     second is a the definition of a variable, including its
11563132718Skan	     initializer.
11564132718Skan
11565132718Skan	     Having seen only the parenthesis, we cannot know which of
11566132718Skan	     these two alternatives should be selected.  Even more
11567132718Skan	     complex are examples like:
11568132718Skan
11569169689Skan	       int i (int (a));
11570132718Skan	       int i (int (3));
11571132718Skan
11572132718Skan	     The former is a function-declaration; the latter is a
11573169689Skan	     variable initialization.
11574132718Skan
11575132718Skan	     Thus again, we try a parameter-declaration-clause, and if
11576132718Skan	     that fails, we back out and return.  */
11577132718Skan
11578132718Skan	  if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11579132718Skan	    {
11580169689Skan	      cp_parameter_declarator *params;
11581132718Skan	      unsigned saved_num_template_parameter_lists;
11582169689Skan
11583146895Skan	      /* In a member-declarator, the only valid interpretation
11584146895Skan		 of a parenthesis is the start of a
11585146895Skan		 parameter-declaration-clause.  (It is invalid to
11586146895Skan		 initialize a static data member with a parenthesized
11587146895Skan		 initializer; only the "=" form of initialization is
11588146895Skan		 permitted.)  */
11589146895Skan	      if (!member_p)
11590146895Skan		cp_parser_parse_tentatively (parser);
11591132718Skan
11592132718Skan	      /* Consume the `('.  */
11593132718Skan	      cp_lexer_consume_token (parser->lexer);
11594132718Skan	      if (first)
11595132718Skan		{
11596132718Skan		  /* If this is going to be an abstract declarator, we're
11597132718Skan		     in a declarator and we can't have default args.  */
11598132718Skan		  parser->default_arg_ok_p = false;
11599132718Skan		  parser->in_declarator_p = true;
11600132718Skan		}
11601169689Skan
11602132718Skan	      /* Inside the function parameter list, surrounding
11603132718Skan		 template-parameter-lists do not apply.  */
11604132718Skan	      saved_num_template_parameter_lists
11605132718Skan		= parser->num_template_parameter_lists;
11606132718Skan	      parser->num_template_parameter_lists = 0;
11607132718Skan
11608132718Skan	      /* Parse the parameter-declaration-clause.  */
11609132718Skan	      params = cp_parser_parameter_declaration_clause (parser);
11610132718Skan
11611132718Skan	      parser->num_template_parameter_lists
11612132718Skan		= saved_num_template_parameter_lists;
11613132718Skan
11614132718Skan	      /* If all went well, parse the cv-qualifier-seq and the
11615169689Skan		 exception-specification.  */
11616146895Skan	      if (member_p || cp_parser_parse_definitely (parser))
11617132718Skan		{
11618169689Skan		  cp_cv_quals cv_quals;
11619132718Skan		  tree exception_specification;
11620132718Skan
11621132718Skan		  if (ctor_dtor_or_conv_p)
11622132718Skan		    *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
11623132718Skan		  first = false;
11624132718Skan		  /* Consume the `)'.  */
11625132718Skan		  cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
11626132718Skan
11627132718Skan		  /* Parse the cv-qualifier-seq.  */
11628169689Skan		  cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11629132718Skan		  /* And the exception-specification.  */
11630169689Skan		  exception_specification
11631132718Skan		    = cp_parser_exception_specification_opt (parser);
11632132718Skan
11633132718Skan		  /* Create the function-declarator.  */
11634132718Skan		  declarator = make_call_declarator (declarator,
11635132718Skan						     params,
11636169689Skan						     cv_quals,
11637132718Skan						     exception_specification);
11638132718Skan		  /* Any subsequent parameter lists are to do with
11639169689Skan		     return type, so are not those of the declared
11640169689Skan		     function.  */
11641132718Skan		  parser->default_arg_ok_p = false;
11642169689Skan
11643132718Skan		  /* Repeat the main loop.  */
11644132718Skan		  continue;
11645132718Skan		}
11646132718Skan	    }
11647169689Skan
11648132718Skan	  /* If this is the first, we can try a parenthesized
11649132718Skan	     declarator.  */
11650132718Skan	  if (first)
11651132718Skan	    {
11652132718Skan	      bool saved_in_type_id_in_expr_p;
11653132718Skan
11654132718Skan	      parser->default_arg_ok_p = saved_default_arg_ok_p;
11655132718Skan	      parser->in_declarator_p = saved_in_declarator_p;
11656169689Skan
11657132718Skan	      /* Consume the `('.  */
11658132718Skan	      cp_lexer_consume_token (parser->lexer);
11659132718Skan	      /* Parse the nested declarator.  */
11660132718Skan	      saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
11661132718Skan	      parser->in_type_id_in_expr_p = true;
11662169689Skan	      declarator
11663132718Skan		= cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
11664146895Skan					/*parenthesized_p=*/NULL,
11665146895Skan					member_p);
11666132718Skan	      parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
11667132718Skan	      first = false;
11668132718Skan	      /* Expect a `)'.  */
11669132718Skan	      if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
11670169689Skan		declarator = cp_error_declarator;
11671169689Skan	      if (declarator == cp_error_declarator)
11672132718Skan		break;
11673169689Skan
11674132718Skan	      goto handle_declarator;
11675132718Skan	    }
11676132718Skan	  /* Otherwise, we must be done.  */
11677132718Skan	  else
11678132718Skan	    break;
11679132718Skan	}
11680132718Skan      else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11681132718Skan	       && token->type == CPP_OPEN_SQUARE)
11682132718Skan	{
11683132718Skan	  /* Parse an array-declarator.  */
11684132718Skan	  tree bounds;
11685132718Skan
11686132718Skan	  if (ctor_dtor_or_conv_p)
11687132718Skan	    *ctor_dtor_or_conv_p = 0;
11688169689Skan
11689132718Skan	  first = false;
11690132718Skan	  parser->default_arg_ok_p = false;
11691132718Skan	  parser->in_declarator_p = true;
11692132718Skan	  /* Consume the `['.  */
11693132718Skan	  cp_lexer_consume_token (parser->lexer);
11694132718Skan	  /* Peek at the next token.  */
11695132718Skan	  token = cp_lexer_peek_token (parser->lexer);
11696132718Skan	  /* If the next token is `]', then there is no
11697132718Skan	     constant-expression.  */
11698132718Skan	  if (token->type != CPP_CLOSE_SQUARE)
11699132718Skan	    {
11700132718Skan	      bool non_constant_p;
11701132718Skan
11702169689Skan	      bounds
11703132718Skan		= cp_parser_constant_expression (parser,
11704132718Skan						 /*allow_non_constant=*/true,
11705132718Skan						 &non_constant_p);
11706132718Skan	      if (!non_constant_p)
11707132718Skan		bounds = fold_non_dependent_expr (bounds);
11708169689Skan	      /* Normally, the array bound must be an integral constant
11709169689Skan		 expression.  However, as an extension, we allow VLAs
11710169689Skan		 in function scopes.  */
11711169689Skan	      else if (!parser->in_function_body)
11712169689Skan		{
11713169689Skan		  error ("array bound is not an integer constant");
11714169689Skan		  bounds = error_mark_node;
11715169689Skan		}
11716132718Skan	    }
11717132718Skan	  else
11718132718Skan	    bounds = NULL_TREE;
11719132718Skan	  /* Look for the closing `]'.  */
11720132718Skan	  if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
11721132718Skan	    {
11722169689Skan	      declarator = cp_error_declarator;
11723132718Skan	      break;
11724132718Skan	    }
11725132718Skan
11726169689Skan	  declarator = make_array_declarator (declarator, bounds);
11727132718Skan	}
11728132718Skan      else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
11729132718Skan	{
11730169689Skan	  tree qualifying_scope;
11731169689Skan	  tree unqualified_name;
11732169689Skan	  special_function_kind sfk;
11733169689Skan	  bool abstract_ok;
11734169689Skan
11735132718Skan	  /* Parse a declarator-id */
11736169689Skan	  abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
11737169689Skan	  if (abstract_ok)
11738132718Skan	    cp_parser_parse_tentatively (parser);
11739169689Skan	  unqualified_name
11740169689Skan	    = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
11741169689Skan	  qualifying_scope = parser->scope;
11742169689Skan	  if (abstract_ok)
11743132718Skan	    {
11744132718Skan	      if (!cp_parser_parse_definitely (parser))
11745169689Skan		unqualified_name = error_mark_node;
11746169689Skan	      else if (unqualified_name
11747169689Skan		       && (qualifying_scope
11748169689Skan			   || (TREE_CODE (unqualified_name)
11749169689Skan			       != IDENTIFIER_NODE)))
11750132718Skan		{
11751132718Skan		  cp_parser_error (parser, "expected unqualified-id");
11752169689Skan		  unqualified_name = error_mark_node;
11753132718Skan		}
11754132718Skan	    }
11755169689Skan
11756169689Skan	  if (!unqualified_name)
11757169689Skan	    return NULL;
11758169689Skan	  if (unqualified_name == error_mark_node)
11759132718Skan	    {
11760169689Skan	      declarator = cp_error_declarator;
11761169689Skan	      break;
11762169689Skan	    }
11763132718Skan
11764169689Skan	  if (qualifying_scope && at_namespace_scope_p ()
11765169689Skan	      && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
11766169689Skan	    {
11767132718Skan	      /* In the declaration of a member of a template class
11768169689Skan		 outside of the class itself, the SCOPE will sometimes
11769169689Skan		 be a TYPENAME_TYPE.  For example, given:
11770132718Skan
11771169689Skan		 template <typename T>
11772169689Skan		 int S<T>::R::i = 3;
11773169689Skan
11774169689Skan		 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
11775169689Skan		 this context, we must resolve S<T>::R to an ordinary
11776169689Skan		 type, rather than a typename type.
11777169689Skan
11778169689Skan		 The reason we normally avoid resolving TYPENAME_TYPEs
11779169689Skan		 is that a specialization of `S' might render
11780169689Skan		 `S<T>::R' not a type.  However, if `S' is
11781169689Skan		 specialized, then this `i' will not be used, so there
11782169689Skan		 is no harm in resolving the types here.  */
11783169689Skan	      tree type;
11784169689Skan
11785169689Skan	      /* Resolve the TYPENAME_TYPE.  */
11786169689Skan	      type = resolve_typename_type (qualifying_scope,
11787169689Skan					    /*only_current_p=*/false);
11788169689Skan	      /* If that failed, the declarator is invalid.  */
11789169689Skan	      if (type == error_mark_node)
11790169689Skan		error ("%<%T::%D%> is not a type",
11791169689Skan		       TYPE_CONTEXT (qualifying_scope),
11792169689Skan		       TYPE_IDENTIFIER (qualifying_scope));
11793169689Skan	      qualifying_scope = type;
11794132718Skan	    }
11795169689Skan
11796169689Skan	  sfk = sfk_none;
11797169689Skan	  if (unqualified_name)
11798132718Skan	    {
11799132718Skan	      tree class_type;
11800132718Skan
11801169689Skan	      if (qualifying_scope
11802169689Skan		  && CLASS_TYPE_P (qualifying_scope))
11803169689Skan		class_type = qualifying_scope;
11804169689Skan	      else
11805169689Skan		class_type = current_class_type;
11806169689Skan
11807169689Skan	      if (TREE_CODE (unqualified_name) == TYPE_DECL)
11808132718Skan		{
11809169689Skan		  tree name_type = TREE_TYPE (unqualified_name);
11810169689Skan		  if (class_type && same_type_p (name_type, class_type))
11811169689Skan		    {
11812169689Skan		      if (qualifying_scope
11813169689Skan			  && CLASSTYPE_USE_TEMPLATE (name_type))
11814169689Skan			{
11815169689Skan			  error ("invalid use of constructor as a template");
11816169689Skan			  inform ("use %<%T::%D%> instead of %<%T::%D%> to "
11817169689Skan				  "name the constructor in a qualified name",
11818169689Skan				  class_type,
11819169689Skan				  DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
11820169689Skan				  class_type, name_type);
11821169689Skan			  declarator = cp_error_declarator;
11822169689Skan			  break;
11823169689Skan			}
11824169689Skan		      else
11825169689Skan			unqualified_name = constructor_name (class_type);
11826169689Skan		    }
11827169689Skan		  else
11828169689Skan		    {
11829169689Skan		      /* We do not attempt to print the declarator
11830169689Skan			 here because we do not have enough
11831169689Skan			 information about its original syntactic
11832169689Skan			 form.  */
11833169689Skan		      cp_parser_error (parser, "invalid declarator");
11834169689Skan		      declarator = cp_error_declarator;
11835169689Skan		      break;
11836169689Skan		    }
11837132718Skan		}
11838169689Skan
11839169689Skan	      if (class_type)
11840132718Skan		{
11841169689Skan		  if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
11842169689Skan		    sfk = sfk_destructor;
11843169689Skan		  else if (IDENTIFIER_TYPENAME_P (unqualified_name))
11844169689Skan		    sfk = sfk_conversion;
11845169689Skan		  else if (/* There's no way to declare a constructor
11846169689Skan			      for an anonymous type, even if the type
11847169689Skan			      got a name for linkage purposes.  */
11848169689Skan			   !TYPE_WAS_ANONYMOUS (class_type)
11849169689Skan			   && constructor_name_p (unqualified_name,
11850169689Skan						  class_type))
11851169689Skan		    {
11852169689Skan		      unqualified_name = constructor_name (class_type);
11853169689Skan		      sfk = sfk_constructor;
11854169689Skan		    }
11855169689Skan
11856169689Skan		  if (ctor_dtor_or_conv_p && sfk != sfk_none)
11857169689Skan		    *ctor_dtor_or_conv_p = -1;
11858132718Skan		}
11859132718Skan	    }
11860169689Skan	  declarator = make_id_declarator (qualifying_scope,
11861169689Skan					   unqualified_name,
11862169689Skan					   sfk);
11863169689Skan	  declarator->id_loc = token->location;
11864132718Skan
11865132718Skan	handle_declarator:;
11866132718Skan	  scope = get_scope_of_declarator (declarator);
11867132718Skan	  if (scope)
11868132718Skan	    /* Any names that appear after the declarator-id for a
11869132718Skan	       member are looked up in the containing scope.  */
11870169689Skan	    pushed_scope = push_scope (scope);
11871132718Skan	  parser->in_declarator_p = true;
11872132718Skan	  if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
11873169689Skan	      || (declarator && declarator->kind == cdk_id))
11874132718Skan	    /* Default args are only allowed on function
11875132718Skan	       declarations.  */
11876132718Skan	    parser->default_arg_ok_p = saved_default_arg_ok_p;
11877132718Skan	  else
11878132718Skan	    parser->default_arg_ok_p = false;
11879132718Skan
11880132718Skan	  first = false;
11881132718Skan	}
11882132718Skan      /* We're done.  */
11883132718Skan      else
11884132718Skan	break;
11885132718Skan    }
11886132718Skan
11887132718Skan  /* For an abstract declarator, we might wind up with nothing at this
11888132718Skan     point.  That's an error; the declarator is not optional.  */
11889132718Skan  if (!declarator)
11890132718Skan    cp_parser_error (parser, "expected declarator");
11891132718Skan
11892132718Skan  /* If we entered a scope, we must exit it now.  */
11893169689Skan  if (pushed_scope)
11894169689Skan    pop_scope (pushed_scope);
11895132718Skan
11896132718Skan  parser->default_arg_ok_p = saved_default_arg_ok_p;
11897132718Skan  parser->in_declarator_p = saved_in_declarator_p;
11898169689Skan
11899132718Skan  return declarator;
11900132718Skan}
11901132718Skan
11902169689Skan/* Parse a ptr-operator.
11903132718Skan
11904132718Skan   ptr-operator:
11905132718Skan     * cv-qualifier-seq [opt]
11906132718Skan     &
11907132718Skan     :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
11908132718Skan
11909132718Skan   GNU Extension:
11910132718Skan
11911132718Skan   ptr-operator:
11912132718Skan     & cv-qualifier-seq [opt]
11913132718Skan
11914169689Skan   Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
11915169689Skan   Returns ADDR_EXPR if a reference was used.  In the case of a
11916169689Skan   pointer-to-member, *TYPE is filled in with the TYPE containing the
11917169689Skan   member.  *CV_QUALS is filled in with the cv-qualifier-seq, or
11918169689Skan   TYPE_UNQUALIFIED, if there are no cv-qualifiers.  Returns
11919169689Skan   ERROR_MARK if an error occurred.  */
11920169689Skan
11921132718Skanstatic enum tree_code
11922169689Skancp_parser_ptr_operator (cp_parser* parser,
11923169689Skan			tree* type,
11924169689Skan			cp_cv_quals *cv_quals)
11925132718Skan{
11926132718Skan  enum tree_code code = ERROR_MARK;
11927132718Skan  cp_token *token;
11928132718Skan
11929132718Skan  /* Assume that it's not a pointer-to-member.  */
11930132718Skan  *type = NULL_TREE;
11931132718Skan  /* And that there are no cv-qualifiers.  */
11932169689Skan  *cv_quals = TYPE_UNQUALIFIED;
11933132718Skan
11934132718Skan  /* Peek at the next token.  */
11935132718Skan  token = cp_lexer_peek_token (parser->lexer);
11936132718Skan  /* If it's a `*' or `&' we have a pointer or reference.  */
11937132718Skan  if (token->type == CPP_MULT || token->type == CPP_AND)
11938132718Skan    {
11939132718Skan      /* Remember which ptr-operator we were processing.  */
11940132718Skan      code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
11941132718Skan
11942132718Skan      /* Consume the `*' or `&'.  */
11943132718Skan      cp_lexer_consume_token (parser->lexer);
11944132718Skan
11945132718Skan      /* A `*' can be followed by a cv-qualifier-seq, and so can a
11946132718Skan	 `&', if we are allowing GNU extensions.  (The only qualifier
11947132718Skan	 that can legally appear after `&' is `restrict', but that is
11948132718Skan	 enforced during semantic analysis.  */
11949169689Skan      if (code == INDIRECT_REF
11950132718Skan	  || cp_parser_allow_gnu_extensions_p (parser))
11951169689Skan	*cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11952132718Skan    }
11953132718Skan  else
11954132718Skan    {
11955132718Skan      /* Try the pointer-to-member case.  */
11956132718Skan      cp_parser_parse_tentatively (parser);
11957132718Skan      /* Look for the optional `::' operator.  */
11958132718Skan      cp_parser_global_scope_opt (parser,
11959132718Skan				  /*current_scope_valid_p=*/false);
11960132718Skan      /* Look for the nested-name specifier.  */
11961132718Skan      cp_parser_nested_name_specifier (parser,
11962132718Skan				       /*typename_keyword_p=*/false,
11963132718Skan				       /*check_dependency_p=*/true,
11964132718Skan				       /*type_p=*/false,
11965132718Skan				       /*is_declaration=*/false);
11966132718Skan      /* If we found it, and the next token is a `*', then we are
11967132718Skan	 indeed looking at a pointer-to-member operator.  */
11968132718Skan      if (!cp_parser_error_occurred (parser)
11969132718Skan	  && cp_parser_require (parser, CPP_MULT, "`*'"))
11970132718Skan	{
11971132718Skan	  /* Indicate that the `*' operator was used.  */
11972132718Skan	  code = INDIRECT_REF;
11973169689Skan
11974169689Skan	  if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
11975169689Skan	    error ("%qD is a namespace", parser->scope);
11976169689Skan	  else
11977169689Skan	    {
11978169689Skan	      /* The type of which the member is a member is given by the
11979169689Skan		 current SCOPE.  */
11980169689Skan	      *type = parser->scope;
11981169689Skan	      /* The next name will not be qualified.  */
11982169689Skan	      parser->scope = NULL_TREE;
11983169689Skan	      parser->qualifying_scope = NULL_TREE;
11984169689Skan	      parser->object_scope = NULL_TREE;
11985169689Skan	      /* Look for the optional cv-qualifier-seq.  */
11986169689Skan	      *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11987169689Skan	    }
11988132718Skan	}
11989132718Skan      /* If that didn't work we don't have a ptr-operator.  */
11990132718Skan      if (!cp_parser_parse_definitely (parser))
11991132718Skan	cp_parser_error (parser, "expected ptr-operator");
11992132718Skan    }
11993132718Skan
11994132718Skan  return code;
11995132718Skan}
11996132718Skan
11997132718Skan/* Parse an (optional) cv-qualifier-seq.
11998132718Skan
11999132718Skan   cv-qualifier-seq:
12000169689Skan     cv-qualifier cv-qualifier-seq [opt]
12001132718Skan
12002169689Skan   cv-qualifier:
12003169689Skan     const
12004169689Skan     volatile
12005132718Skan
12006169689Skan   GNU Extension:
12007169689Skan
12008169689Skan   cv-qualifier:
12009169689Skan     __restrict__
12010169689Skan
12011169689Skan   Returns a bitmask representing the cv-qualifiers.  */
12012169689Skan
12013169689Skanstatic cp_cv_quals
12014132718Skancp_parser_cv_qualifier_seq_opt (cp_parser* parser)
12015132718Skan{
12016169689Skan  cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
12017169689Skan
12018132718Skan  while (true)
12019132718Skan    {
12020169689Skan      cp_token *token;
12021169689Skan      cp_cv_quals cv_qualifier;
12022132718Skan
12023169689Skan      /* Peek at the next token.  */
12024169689Skan      token = cp_lexer_peek_token (parser->lexer);
12025169689Skan      /* See if it's a cv-qualifier.  */
12026169689Skan      switch (token->keyword)
12027169689Skan	{
12028169689Skan	case RID_CONST:
12029169689Skan	  cv_qualifier = TYPE_QUAL_CONST;
12030169689Skan	  break;
12031132718Skan
12032169689Skan	case RID_VOLATILE:
12033169689Skan	  cv_qualifier = TYPE_QUAL_VOLATILE;
12034169689Skan	  break;
12035132718Skan
12036169689Skan	case RID_RESTRICT:
12037169689Skan	  cv_qualifier = TYPE_QUAL_RESTRICT;
12038169689Skan	  break;
12039132718Skan
12040169689Skan	default:
12041169689Skan	  cv_qualifier = TYPE_UNQUALIFIED;
12042169689Skan	  break;
12043169689Skan	}
12044132718Skan
12045169689Skan      if (!cv_qualifier)
12046169689Skan	break;
12047132718Skan
12048169689Skan      if (cv_quals & cv_qualifier)
12049169689Skan	{
12050169689Skan	  error ("duplicate cv-qualifier");
12051169689Skan	  cp_lexer_purge_token (parser->lexer);
12052169689Skan	}
12053169689Skan      else
12054169689Skan	{
12055169689Skan	  cp_lexer_consume_token (parser->lexer);
12056169689Skan	  cv_quals |= cv_qualifier;
12057169689Skan	}
12058132718Skan    }
12059132718Skan
12060169689Skan  return cv_quals;
12061132718Skan}
12062132718Skan
12063132718Skan/* Parse a declarator-id.
12064132718Skan
12065132718Skan   declarator-id:
12066132718Skan     id-expression
12067169689Skan     :: [opt] nested-name-specifier [opt] type-name
12068132718Skan
12069132718Skan   In the `id-expression' case, the value returned is as for
12070132718Skan   cp_parser_id_expression if the id-expression was an unqualified-id.
12071132718Skan   If the id-expression was a qualified-id, then a SCOPE_REF is
12072132718Skan   returned.  The first operand is the scope (either a NAMESPACE_DECL
12073132718Skan   or TREE_TYPE), but the second is still just a representation of an
12074132718Skan   unqualified-id.  */
12075132718Skan
12076132718Skanstatic tree
12077169689Skancp_parser_declarator_id (cp_parser* parser, bool optional_p)
12078132718Skan{
12079169689Skan  tree id;
12080132718Skan  /* The expression must be an id-expression.  Assume that qualified
12081132718Skan     names are the names of types so that:
12082132718Skan
12083132718Skan       template <class T>
12084132718Skan       int S<T>::R::i = 3;
12085132718Skan
12086132718Skan     will work; we must treat `S<T>::R' as the name of a type.
12087132718Skan     Similarly, assume that qualified names are templates, where
12088132718Skan     required, so that:
12089132718Skan
12090132718Skan       template <class T>
12091132718Skan       int S<T>::R<T>::i = 3;
12092132718Skan
12093132718Skan     will work, too.  */
12094169689Skan  id = cp_parser_id_expression (parser,
12095169689Skan				/*template_keyword_p=*/false,
12096169689Skan				/*check_dependency_p=*/false,
12097169689Skan				/*template_p=*/NULL,
12098169689Skan				/*declarator_p=*/true,
12099169689Skan				optional_p);
12100169689Skan  if (id && BASELINK_P (id))
12101169689Skan    id = BASELINK_FUNCTIONS (id);
12102169689Skan  return id;
12103132718Skan}
12104132718Skan
12105132718Skan/* Parse a type-id.
12106132718Skan
12107132718Skan   type-id:
12108132718Skan     type-specifier-seq abstract-declarator [opt]
12109132718Skan
12110132718Skan   Returns the TYPE specified.  */
12111132718Skan
12112132718Skanstatic tree
12113132718Skancp_parser_type_id (cp_parser* parser)
12114132718Skan{
12115169689Skan  cp_decl_specifier_seq type_specifier_seq;
12116169689Skan  cp_declarator *abstract_declarator;
12117132718Skan
12118132718Skan  /* Parse the type-specifier-seq.  */
12119169689Skan  cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
12120169689Skan				&type_specifier_seq);
12121169689Skan  if (type_specifier_seq.type == error_mark_node)
12122132718Skan    return error_mark_node;
12123132718Skan
12124132718Skan  /* There might or might not be an abstract declarator.  */
12125132718Skan  cp_parser_parse_tentatively (parser);
12126132718Skan  /* Look for the declarator.  */
12127169689Skan  abstract_declarator
12128132718Skan    = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
12129146895Skan			    /*parenthesized_p=*/NULL,
12130146895Skan			    /*member_p=*/false);
12131132718Skan  /* Check to see if there really was a declarator.  */
12132132718Skan  if (!cp_parser_parse_definitely (parser))
12133169689Skan    abstract_declarator = NULL;
12134132718Skan
12135169689Skan  return groktypename (&type_specifier_seq, abstract_declarator);
12136132718Skan}
12137132718Skan
12138132718Skan/* Parse a type-specifier-seq.
12139132718Skan
12140132718Skan   type-specifier-seq:
12141132718Skan     type-specifier type-specifier-seq [opt]
12142132718Skan
12143132718Skan   GNU extension:
12144132718Skan
12145132718Skan   type-specifier-seq:
12146132718Skan     attributes type-specifier-seq [opt]
12147132718Skan
12148169689Skan   If IS_CONDITION is true, we are at the start of a "condition",
12149169689Skan   e.g., we've just seen "if (".
12150132718Skan
12151169689Skan   Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
12152169689Skan
12153169689Skanstatic void
12154169689Skancp_parser_type_specifier_seq (cp_parser* parser,
12155169689Skan			      bool is_condition,
12156169689Skan			      cp_decl_specifier_seq *type_specifier_seq)
12157132718Skan{
12158132718Skan  bool seen_type_specifier = false;
12159169689Skan  cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
12160132718Skan
12161169689Skan  /* Clear the TYPE_SPECIFIER_SEQ.  */
12162169689Skan  clear_decl_specs (type_specifier_seq);
12163169689Skan
12164132718Skan  /* Parse the type-specifiers and attributes.  */
12165132718Skan  while (true)
12166132718Skan    {
12167132718Skan      tree type_specifier;
12168169689Skan      bool is_cv_qualifier;
12169132718Skan
12170132718Skan      /* Check for attributes first.  */
12171132718Skan      if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
12172132718Skan	{
12173169689Skan	  type_specifier_seq->attributes =
12174169689Skan	    chainon (type_specifier_seq->attributes,
12175169689Skan		     cp_parser_attributes_opt (parser));
12176132718Skan	  continue;
12177132718Skan	}
12178132718Skan
12179132718Skan      /* Look for the type-specifier.  */
12180169689Skan      type_specifier = cp_parser_type_specifier (parser,
12181169689Skan						 flags,
12182169689Skan						 type_specifier_seq,
12183132718Skan						 /*is_declaration=*/false,
12184132718Skan						 NULL,
12185169689Skan						 &is_cv_qualifier);
12186169689Skan      if (!type_specifier)
12187169689Skan	{
12188169689Skan	  /* If the first type-specifier could not be found, this is not a
12189169689Skan	     type-specifier-seq at all.  */
12190169689Skan	  if (!seen_type_specifier)
12191169689Skan	    {
12192169689Skan	      cp_parser_error (parser, "expected type-specifier");
12193169689Skan	      type_specifier_seq->type = error_mark_node;
12194169689Skan	      return;
12195169689Skan	    }
12196169689Skan	  /* If subsequent type-specifiers could not be found, the
12197169689Skan	     type-specifier-seq is complete.  */
12198169689Skan	  break;
12199169689Skan	}
12200132718Skan
12201132718Skan      seen_type_specifier = true;
12202169689Skan      /* The standard says that a condition can be:
12203169689Skan
12204169689Skan	    type-specifier-seq declarator = assignment-expression
12205169689Skan
12206169689Skan	 However, given:
12207169689Skan
12208169689Skan	   struct S {};
12209169689Skan	   if (int S = ...)
12210169689Skan
12211169689Skan	 we should treat the "S" as a declarator, not as a
12212169689Skan	 type-specifier.  The standard doesn't say that explicitly for
12213169689Skan	 type-specifier-seq, but it does say that for
12214169689Skan	 decl-specifier-seq in an ordinary declaration.  Perhaps it
12215169689Skan	 would be clearer just to allow a decl-specifier-seq here, and
12216169689Skan	 then add a semantic restriction that if any decl-specifiers
12217169689Skan	 that are not type-specifiers appear, the program is invalid.  */
12218169689Skan      if (is_condition && !is_cv_qualifier)
12219169689Skan	flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
12220132718Skan    }
12221132718Skan
12222169689Skan  cp_parser_check_decl_spec (type_specifier_seq);
12223132718Skan}
12224132718Skan
12225132718Skan/* Parse a parameter-declaration-clause.
12226132718Skan
12227132718Skan   parameter-declaration-clause:
12228132718Skan     parameter-declaration-list [opt] ... [opt]
12229132718Skan     parameter-declaration-list , ...
12230132718Skan
12231169689Skan   Returns a representation for the parameter declarations.  A return
12232169689Skan   value of NULL indicates a parameter-declaration-clause consisting
12233169689Skan   only of an ellipsis.  */
12234132718Skan
12235169689Skanstatic cp_parameter_declarator *
12236132718Skancp_parser_parameter_declaration_clause (cp_parser* parser)
12237132718Skan{
12238169689Skan  cp_parameter_declarator *parameters;
12239132718Skan  cp_token *token;
12240132718Skan  bool ellipsis_p;
12241169689Skan  bool is_error;
12242132718Skan
12243132718Skan  /* Peek at the next token.  */
12244132718Skan  token = cp_lexer_peek_token (parser->lexer);
12245132718Skan  /* Check for trivial parameter-declaration-clauses.  */
12246132718Skan  if (token->type == CPP_ELLIPSIS)
12247132718Skan    {
12248132718Skan      /* Consume the `...' token.  */
12249132718Skan      cp_lexer_consume_token (parser->lexer);
12250169689Skan      return NULL;
12251132718Skan    }
12252132718Skan  else if (token->type == CPP_CLOSE_PAREN)
12253132718Skan    /* There are no parameters.  */
12254132718Skan    {
12255132718Skan#ifndef NO_IMPLICIT_EXTERN_C
12256132718Skan      if (in_system_header && current_class_type == NULL
12257132718Skan	  && current_lang_name == lang_name_c)
12258169689Skan	return NULL;
12259132718Skan      else
12260132718Skan#endif
12261169689Skan	return no_parameters;
12262132718Skan    }
12263132718Skan  /* Check for `(void)', too, which is a special case.  */
12264132718Skan  else if (token->keyword == RID_VOID
12265169689Skan	   && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
12266132718Skan	       == CPP_CLOSE_PAREN))
12267132718Skan    {
12268132718Skan      /* Consume the `void' token.  */
12269132718Skan      cp_lexer_consume_token (parser->lexer);
12270132718Skan      /* There are no parameters.  */
12271169689Skan      return no_parameters;
12272132718Skan    }
12273169689Skan
12274132718Skan  /* Parse the parameter-declaration-list.  */
12275169689Skan  parameters = cp_parser_parameter_declaration_list (parser, &is_error);
12276132718Skan  /* If a parse error occurred while parsing the
12277132718Skan     parameter-declaration-list, then the entire
12278132718Skan     parameter-declaration-clause is erroneous.  */
12279169689Skan  if (is_error)
12280169689Skan    return NULL;
12281132718Skan
12282132718Skan  /* Peek at the next token.  */
12283132718Skan  token = cp_lexer_peek_token (parser->lexer);
12284132718Skan  /* If it's a `,', the clause should terminate with an ellipsis.  */
12285132718Skan  if (token->type == CPP_COMMA)
12286132718Skan    {
12287132718Skan      /* Consume the `,'.  */
12288132718Skan      cp_lexer_consume_token (parser->lexer);
12289132718Skan      /* Expect an ellipsis.  */
12290169689Skan      ellipsis_p
12291132718Skan	= (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
12292132718Skan    }
12293169689Skan  /* It might also be `...' if the optional trailing `,' was
12294132718Skan     omitted.  */
12295132718Skan  else if (token->type == CPP_ELLIPSIS)
12296132718Skan    {
12297132718Skan      /* Consume the `...' token.  */
12298132718Skan      cp_lexer_consume_token (parser->lexer);
12299132718Skan      /* And remember that we saw it.  */
12300132718Skan      ellipsis_p = true;
12301132718Skan    }
12302132718Skan  else
12303132718Skan    ellipsis_p = false;
12304132718Skan
12305132718Skan  /* Finish the parameter list.  */
12306169689Skan  if (parameters && ellipsis_p)
12307169689Skan    parameters->ellipsis_p = true;
12308169689Skan
12309169689Skan  return parameters;
12310132718Skan}
12311132718Skan
12312132718Skan/* Parse a parameter-declaration-list.
12313132718Skan
12314132718Skan   parameter-declaration-list:
12315132718Skan     parameter-declaration
12316132718Skan     parameter-declaration-list , parameter-declaration
12317132718Skan
12318132718Skan   Returns a representation of the parameter-declaration-list, as for
12319132718Skan   cp_parser_parameter_declaration_clause.  However, the
12320169689Skan   `void_list_node' is never appended to the list.  Upon return,
12321169689Skan   *IS_ERROR will be true iff an error occurred.  */
12322132718Skan
12323169689Skanstatic cp_parameter_declarator *
12324169689Skancp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
12325132718Skan{
12326169689Skan  cp_parameter_declarator *parameters = NULL;
12327169689Skan  cp_parameter_declarator **tail = &parameters;
12328169689Skan  bool saved_in_unbraced_linkage_specification_p;
12329132718Skan
12330169689Skan  /* Assume all will go well.  */
12331169689Skan  *is_error = false;
12332169689Skan  /* The special considerations that apply to a function within an
12333169689Skan     unbraced linkage specifications do not apply to the parameters
12334169689Skan     to the function.  */
12335169689Skan  saved_in_unbraced_linkage_specification_p
12336169689Skan    = parser->in_unbraced_linkage_specification_p;
12337169689Skan  parser->in_unbraced_linkage_specification_p = false;
12338169689Skan
12339132718Skan  /* Look for more parameters.  */
12340132718Skan  while (true)
12341132718Skan    {
12342169689Skan      cp_parameter_declarator *parameter;
12343132718Skan      bool parenthesized_p;
12344132718Skan      /* Parse the parameter.  */
12345169689Skan      parameter
12346169689Skan	= cp_parser_parameter_declaration (parser,
12347132718Skan					   /*template_parm_p=*/false,
12348132718Skan					   &parenthesized_p);
12349132718Skan
12350132718Skan      /* If a parse error occurred parsing the parameter declaration,
12351132718Skan	 then the entire parameter-declaration-list is erroneous.  */
12352169689Skan      if (!parameter)
12353132718Skan	{
12354169689Skan	  *is_error = true;
12355169689Skan	  parameters = NULL;
12356132718Skan	  break;
12357132718Skan	}
12358132718Skan      /* Add the new parameter to the list.  */
12359169689Skan      *tail = parameter;
12360169689Skan      tail = &parameter->next;
12361132718Skan
12362132718Skan      /* Peek at the next token.  */
12363132718Skan      if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
12364169689Skan	  || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
12365169689Skan	  /* These are for Objective-C++ */
12366169689Skan	  || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
12367169689Skan	  || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12368132718Skan	/* The parameter-declaration-list is complete.  */
12369132718Skan	break;
12370132718Skan      else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12371132718Skan	{
12372132718Skan	  cp_token *token;
12373132718Skan
12374132718Skan	  /* Peek at the next token.  */
12375132718Skan	  token = cp_lexer_peek_nth_token (parser->lexer, 2);
12376132718Skan	  /* If it's an ellipsis, then the list is complete.  */
12377132718Skan	  if (token->type == CPP_ELLIPSIS)
12378132718Skan	    break;
12379132718Skan	  /* Otherwise, there must be more parameters.  Consume the
12380132718Skan	     `,'.  */
12381132718Skan	  cp_lexer_consume_token (parser->lexer);
12382132718Skan	  /* When parsing something like:
12383132718Skan
12384169689Skan		int i(float f, double d)
12385169689Skan
12386169689Skan	     we can tell after seeing the declaration for "f" that we
12387132718Skan	     are not looking at an initialization of a variable "i",
12388169689Skan	     but rather at the declaration of a function "i".
12389132718Skan
12390132718Skan	     Due to the fact that the parsing of template arguments
12391132718Skan	     (as specified to a template-id) requires backtracking we
12392132718Skan	     cannot use this technique when inside a template argument
12393132718Skan	     list.  */
12394132718Skan	  if (!parser->in_template_argument_list_p
12395132718Skan	      && !parser->in_type_id_in_expr_p
12396169689Skan	      && cp_parser_uncommitted_to_tentative_parse_p (parser)
12397132718Skan	      /* However, a parameter-declaration of the form
12398132718Skan		 "foat(f)" (which is a valid declaration of a
12399132718Skan		 parameter "f") can also be interpreted as an
12400132718Skan		 expression (the conversion of "f" to "float").  */
12401132718Skan	      && !parenthesized_p)
12402132718Skan	    cp_parser_commit_to_tentative_parse (parser);
12403132718Skan	}
12404132718Skan      else
12405132718Skan	{
12406169689Skan	  cp_parser_error (parser, "expected %<,%> or %<...%>");
12407169689Skan	  if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
12408169689Skan	    cp_parser_skip_to_closing_parenthesis (parser,
12409132718Skan						   /*recovering=*/true,
12410132718Skan						   /*or_comma=*/false,
12411132718Skan						   /*consume_paren=*/false);
12412132718Skan	  break;
12413132718Skan	}
12414132718Skan    }
12415132718Skan
12416169689Skan  parser->in_unbraced_linkage_specification_p
12417169689Skan    = saved_in_unbraced_linkage_specification_p;
12418169689Skan
12419169689Skan  return parameters;
12420132718Skan}
12421132718Skan
12422132718Skan/* Parse a parameter declaration.
12423132718Skan
12424132718Skan   parameter-declaration:
12425132718Skan     decl-specifier-seq declarator
12426132718Skan     decl-specifier-seq declarator = assignment-expression
12427132718Skan     decl-specifier-seq abstract-declarator [opt]
12428132718Skan     decl-specifier-seq abstract-declarator [opt] = assignment-expression
12429132718Skan
12430132718Skan   If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
12431132718Skan   declares a template parameter.  (In that case, a non-nested `>'
12432132718Skan   token encountered during the parsing of the assignment-expression
12433132718Skan   is not interpreted as a greater-than operator.)
12434132718Skan
12435169689Skan   Returns a representation of the parameter, or NULL if an error
12436169689Skan   occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
12437169689Skan   true iff the declarator is of the form "(p)".  */
12438132718Skan
12439169689Skanstatic cp_parameter_declarator *
12440169689Skancp_parser_parameter_declaration (cp_parser *parser,
12441132718Skan				 bool template_parm_p,
12442132718Skan				 bool *parenthesized_p)
12443132718Skan{
12444132718Skan  int declares_class_or_enum;
12445132718Skan  bool greater_than_is_operator_p;
12446169689Skan  cp_decl_specifier_seq decl_specifiers;
12447169689Skan  cp_declarator *declarator;
12448132718Skan  tree default_argument;
12449132718Skan  cp_token *token;
12450132718Skan  const char *saved_message;
12451132718Skan
12452132718Skan  /* In a template parameter, `>' is not an operator.
12453132718Skan
12454132718Skan     [temp.param]
12455132718Skan
12456132718Skan     When parsing a default template-argument for a non-type
12457132718Skan     template-parameter, the first non-nested `>' is taken as the end
12458132718Skan     of the template parameter-list rather than a greater-than
12459132718Skan     operator.  */
12460132718Skan  greater_than_is_operator_p = !template_parm_p;
12461132718Skan
12462132718Skan  /* Type definitions may not appear in parameter types.  */
12463132718Skan  saved_message = parser->type_definition_forbidden_message;
12464169689Skan  parser->type_definition_forbidden_message
12465132718Skan    = "types may not be defined in parameter types";
12466132718Skan
12467132718Skan  /* Parse the declaration-specifiers.  */
12468169689Skan  cp_parser_decl_specifier_seq (parser,
12469169689Skan				CP_PARSER_FLAGS_NONE,
12470169689Skan				&decl_specifiers,
12471169689Skan				&declares_class_or_enum);
12472132718Skan  /* If an error occurred, there's no reason to attempt to parse the
12473132718Skan     rest of the declaration.  */
12474132718Skan  if (cp_parser_error_occurred (parser))
12475132718Skan    {
12476132718Skan      parser->type_definition_forbidden_message = saved_message;
12477169689Skan      return NULL;
12478132718Skan    }
12479132718Skan
12480132718Skan  /* Peek at the next token.  */
12481132718Skan  token = cp_lexer_peek_token (parser->lexer);
12482132718Skan  /* If the next token is a `)', `,', `=', `>', or `...', then there
12483132718Skan     is no declarator.  */
12484169689Skan  if (token->type == CPP_CLOSE_PAREN
12485132718Skan      || token->type == CPP_COMMA
12486132718Skan      || token->type == CPP_EQ
12487132718Skan      || token->type == CPP_ELLIPSIS
12488132718Skan      || token->type == CPP_GREATER)
12489132718Skan    {
12490169689Skan      declarator = NULL;
12491132718Skan      if (parenthesized_p)
12492132718Skan	*parenthesized_p = false;
12493132718Skan    }
12494132718Skan  /* Otherwise, there should be a declarator.  */
12495132718Skan  else
12496132718Skan    {
12497132718Skan      bool saved_default_arg_ok_p = parser->default_arg_ok_p;
12498132718Skan      parser->default_arg_ok_p = false;
12499169689Skan
12500132718Skan      /* After seeing a decl-specifier-seq, if the next token is not a
12501132718Skan	 "(", there is no possibility that the code is a valid
12502132718Skan	 expression.  Therefore, if parsing tentatively, we commit at
12503132718Skan	 this point.  */
12504132718Skan      if (!parser->in_template_argument_list_p
12505132718Skan	  /* In an expression context, having seen:
12506132718Skan
12507132718Skan	       (int((char ...
12508132718Skan
12509132718Skan	     we cannot be sure whether we are looking at a
12510132718Skan	     function-type (taking a "char" as a parameter) or a cast
12511132718Skan	     of some object of type "char" to "int".  */
12512132718Skan	  && !parser->in_type_id_in_expr_p
12513169689Skan	  && cp_parser_uncommitted_to_tentative_parse_p (parser)
12514132718Skan	  && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
12515132718Skan	cp_parser_commit_to_tentative_parse (parser);
12516132718Skan      /* Parse the declarator.  */
12517132718Skan      declarator = cp_parser_declarator (parser,
12518132718Skan					 CP_PARSER_DECLARATOR_EITHER,
12519132718Skan					 /*ctor_dtor_or_conv_p=*/NULL,
12520146895Skan					 parenthesized_p,
12521146895Skan					 /*member_p=*/false);
12522132718Skan      parser->default_arg_ok_p = saved_default_arg_ok_p;
12523132718Skan      /* After the declarator, allow more attributes.  */
12524169689Skan      decl_specifiers.attributes
12525169689Skan	= chainon (decl_specifiers.attributes,
12526169689Skan		   cp_parser_attributes_opt (parser));
12527132718Skan    }
12528132718Skan
12529132718Skan  /* The restriction on defining new types applies only to the type
12530132718Skan     of the parameter, not to the default argument.  */
12531132718Skan  parser->type_definition_forbidden_message = saved_message;
12532132718Skan
12533132718Skan  /* If the next token is `=', then process a default argument.  */
12534132718Skan  if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12535132718Skan    {
12536132718Skan      bool saved_greater_than_is_operator_p;
12537132718Skan      /* Consume the `='.  */
12538132718Skan      cp_lexer_consume_token (parser->lexer);
12539132718Skan
12540132718Skan      /* If we are defining a class, then the tokens that make up the
12541132718Skan	 default argument must be saved and processed later.  */
12542169689Skan      if (!template_parm_p && at_class_scope_p ()
12543132718Skan	  && TYPE_BEING_DEFINED (current_class_type))
12544132718Skan	{
12545132718Skan	  unsigned depth = 0;
12546169689Skan	  cp_token *first_token;
12547169689Skan	  cp_token *token;
12548132718Skan
12549132718Skan	  /* Add tokens until we have processed the entire default
12550169689Skan	     argument.  We add the range [first_token, token).  */
12551169689Skan	  first_token = cp_lexer_peek_token (parser->lexer);
12552132718Skan	  while (true)
12553132718Skan	    {
12554132718Skan	      bool done = false;
12555132718Skan
12556132718Skan	      /* Peek at the next token.  */
12557132718Skan	      token = cp_lexer_peek_token (parser->lexer);
12558132718Skan	      /* What we do depends on what token we have.  */
12559132718Skan	      switch (token->type)
12560132718Skan		{
12561132718Skan		  /* In valid code, a default argument must be
12562132718Skan		     immediately followed by a `,' `)', or `...'.  */
12563132718Skan		case CPP_COMMA:
12564132718Skan		case CPP_CLOSE_PAREN:
12565132718Skan		case CPP_ELLIPSIS:
12566132718Skan		  /* If we run into a non-nested `;', `}', or `]',
12567132718Skan		     then the code is invalid -- but the default
12568132718Skan		     argument is certainly over.  */
12569132718Skan		case CPP_SEMICOLON:
12570132718Skan		case CPP_CLOSE_BRACE:
12571132718Skan		case CPP_CLOSE_SQUARE:
12572132718Skan		  if (depth == 0)
12573132718Skan		    done = true;
12574132718Skan		  /* Update DEPTH, if necessary.  */
12575132718Skan		  else if (token->type == CPP_CLOSE_PAREN
12576132718Skan			   || token->type == CPP_CLOSE_BRACE
12577132718Skan			   || token->type == CPP_CLOSE_SQUARE)
12578132718Skan		    --depth;
12579132718Skan		  break;
12580132718Skan
12581132718Skan		case CPP_OPEN_PAREN:
12582132718Skan		case CPP_OPEN_SQUARE:
12583132718Skan		case CPP_OPEN_BRACE:
12584132718Skan		  ++depth;
12585132718Skan		  break;
12586132718Skan
12587132718Skan		case CPP_GREATER:
12588132718Skan		  /* If we see a non-nested `>', and `>' is not an
12589132718Skan		     operator, then it marks the end of the default
12590132718Skan		     argument.  */
12591132718Skan		  if (!depth && !greater_than_is_operator_p)
12592132718Skan		    done = true;
12593132718Skan		  break;
12594132718Skan
12595132718Skan		  /* If we run out of tokens, issue an error message.  */
12596132718Skan		case CPP_EOF:
12597169689Skan		case CPP_PRAGMA_EOL:
12598132718Skan		  error ("file ends in default argument");
12599132718Skan		  done = true;
12600132718Skan		  break;
12601132718Skan
12602132718Skan		case CPP_NAME:
12603132718Skan		case CPP_SCOPE:
12604132718Skan		  /* In these cases, we should look for template-ids.
12605169689Skan		     For example, if the default argument is
12606132718Skan		     `X<int, double>()', we need to do name lookup to
12607132718Skan		     figure out whether or not `X' is a template; if
12608132718Skan		     so, the `,' does not end the default argument.
12609132718Skan
12610132718Skan		     That is not yet done.  */
12611132718Skan		  break;
12612132718Skan
12613132718Skan		default:
12614132718Skan		  break;
12615132718Skan		}
12616132718Skan
12617132718Skan	      /* If we've reached the end, stop.  */
12618132718Skan	      if (done)
12619132718Skan		break;
12620169689Skan
12621132718Skan	      /* Add the token to the token block.  */
12622132718Skan	      token = cp_lexer_consume_token (parser->lexer);
12623132718Skan	    }
12624169689Skan
12625169689Skan	  /* Create a DEFAULT_ARG to represented the unparsed default
12626169689Skan	     argument.  */
12627169689Skan	  default_argument = make_node (DEFAULT_ARG);
12628169689Skan	  DEFARG_TOKENS (default_argument)
12629169689Skan	    = cp_token_cache_new (first_token, token);
12630169689Skan	  DEFARG_INSTANTIATIONS (default_argument) = NULL;
12631132718Skan	}
12632132718Skan      /* Outside of a class definition, we can just parse the
12633169689Skan	 assignment-expression.  */
12634132718Skan      else
12635132718Skan	{
12636132718Skan	  bool saved_local_variables_forbidden_p;
12637132718Skan
12638132718Skan	  /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
12639132718Skan	     set correctly.  */
12640169689Skan	  saved_greater_than_is_operator_p
12641132718Skan	    = parser->greater_than_is_operator_p;
12642132718Skan	  parser->greater_than_is_operator_p = greater_than_is_operator_p;
12643132718Skan	  /* Local variable names (and the `this' keyword) may not
12644132718Skan	     appear in a default argument.  */
12645169689Skan	  saved_local_variables_forbidden_p
12646132718Skan	    = parser->local_variables_forbidden_p;
12647132718Skan	  parser->local_variables_forbidden_p = true;
12648169689Skan	  /* The default argument expression may cause implicitly
12649169689Skan	     defined member functions to be synthesized, which will
12650169689Skan	     result in garbage collection.  We must treat this
12651169689Skan	     situation as if we were within the body of function so as
12652169689Skan	     to avoid collecting live data on the stack.  */
12653169689Skan	  ++function_depth;
12654132718Skan	  /* Parse the assignment-expression.  */
12655169689Skan	  if (template_parm_p)
12656169689Skan	    push_deferring_access_checks (dk_no_deferred);
12657169689Skan	  default_argument
12658169689Skan	    = cp_parser_assignment_expression (parser, /*cast_p=*/false);
12659169689Skan	  if (template_parm_p)
12660169689Skan	    pop_deferring_access_checks ();
12661132718Skan	  /* Restore saved state.  */
12662169689Skan	  --function_depth;
12663169689Skan	  parser->greater_than_is_operator_p
12664132718Skan	    = saved_greater_than_is_operator_p;
12665169689Skan	  parser->local_variables_forbidden_p
12666169689Skan	    = saved_local_variables_forbidden_p;
12667132718Skan	}
12668132718Skan      if (!parser->default_arg_ok_p)
12669132718Skan	{
12670132718Skan	  if (!flag_pedantic_errors)
12671169689Skan	    warning (0, "deprecated use of default argument for parameter of non-function");
12672132718Skan	  else
12673132718Skan	    {
12674132718Skan	      error ("default arguments are only permitted for function parameters");
12675132718Skan	      default_argument = NULL_TREE;
12676132718Skan	    }
12677132718Skan	}
12678132718Skan    }
12679132718Skan  else
12680132718Skan    default_argument = NULL_TREE;
12681132718Skan
12682169689Skan  return make_parameter_declarator (&decl_specifiers,
12683169689Skan				    declarator,
12684169689Skan				    default_argument);
12685132718Skan}
12686132718Skan
12687132718Skan/* Parse a function-body.
12688132718Skan
12689132718Skan   function-body:
12690132718Skan     compound_statement  */
12691132718Skan
12692132718Skanstatic void
12693132718Skancp_parser_function_body (cp_parser *parser)
12694132718Skan{
12695169689Skan  cp_parser_compound_statement (parser, NULL, false);
12696132718Skan}
12697132718Skan
12698132718Skan/* Parse a ctor-initializer-opt followed by a function-body.  Return
12699132718Skan   true if a ctor-initializer was present.  */
12700132718Skan
12701132718Skanstatic bool
12702132718Skancp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
12703132718Skan{
12704132718Skan  tree body;
12705132718Skan  bool ctor_initializer_p;
12706132718Skan
12707132718Skan  /* Begin the function body.  */
12708132718Skan  body = begin_function_body ();
12709132718Skan  /* Parse the optional ctor-initializer.  */
12710132718Skan  ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
12711132718Skan  /* Parse the function-body.  */
12712132718Skan  cp_parser_function_body (parser);
12713132718Skan  /* Finish the function body.  */
12714132718Skan  finish_function_body (body);
12715132718Skan
12716132718Skan  return ctor_initializer_p;
12717132718Skan}
12718132718Skan
12719132718Skan/* Parse an initializer.
12720132718Skan
12721132718Skan   initializer:
12722132718Skan     = initializer-clause
12723169689Skan     ( expression-list )
12724132718Skan
12725169689Skan   Returns an expression representing the initializer.  If no
12726169689Skan   initializer is present, NULL_TREE is returned.
12727132718Skan
12728132718Skan   *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
12729132718Skan   production is used, and zero otherwise.  *IS_PARENTHESIZED_INIT is
12730132718Skan   set to FALSE if there is no initializer present.  If there is an
12731132718Skan   initializer, and it is not a constant-expression, *NON_CONSTANT_P
12732132718Skan   is set to true; otherwise it is set to false.  */
12733132718Skan
12734132718Skanstatic tree
12735132718Skancp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
12736132718Skan		       bool* non_constant_p)
12737132718Skan{
12738132718Skan  cp_token *token;
12739132718Skan  tree init;
12740132718Skan
12741132718Skan  /* Peek at the next token.  */
12742132718Skan  token = cp_lexer_peek_token (parser->lexer);
12743132718Skan
12744132718Skan  /* Let our caller know whether or not this initializer was
12745132718Skan     parenthesized.  */
12746132718Skan  *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
12747132718Skan  /* Assume that the initializer is constant.  */
12748132718Skan  *non_constant_p = false;
12749132718Skan
12750132718Skan  if (token->type == CPP_EQ)
12751132718Skan    {
12752132718Skan      /* Consume the `='.  */
12753132718Skan      cp_lexer_consume_token (parser->lexer);
12754132718Skan      /* Parse the initializer-clause.  */
12755132718Skan      init = cp_parser_initializer_clause (parser, non_constant_p);
12756132718Skan    }
12757132718Skan  else if (token->type == CPP_OPEN_PAREN)
12758132718Skan    init = cp_parser_parenthesized_expression_list (parser, false,
12759169689Skan						    /*cast_p=*/false,
12760132718Skan						    non_constant_p);
12761132718Skan  else
12762132718Skan    {
12763132718Skan      /* Anything else is an error.  */
12764132718Skan      cp_parser_error (parser, "expected initializer");
12765132718Skan      init = error_mark_node;
12766132718Skan    }
12767132718Skan
12768132718Skan  return init;
12769132718Skan}
12770132718Skan
12771169689Skan/* Parse an initializer-clause.
12772132718Skan
12773132718Skan   initializer-clause:
12774132718Skan     assignment-expression
12775132718Skan     { initializer-list , [opt] }
12776132718Skan     { }
12777132718Skan
12778169689Skan   Returns an expression representing the initializer.
12779132718Skan
12780132718Skan   If the `assignment-expression' production is used the value
12781169689Skan   returned is simply a representation for the expression.
12782132718Skan
12783132718Skan   Otherwise, a CONSTRUCTOR is returned.  The CONSTRUCTOR_ELTS will be
12784169689Skan   the elements of the initializer-list (or NULL, if the last
12785132718Skan   production is used).  The TREE_TYPE for the CONSTRUCTOR will be
12786132718Skan   NULL_TREE.  There is no way to detect whether or not the optional
12787132718Skan   trailing `,' was provided.  NON_CONSTANT_P is as for
12788132718Skan   cp_parser_initializer.  */
12789132718Skan
12790132718Skanstatic tree
12791132718Skancp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
12792132718Skan{
12793169689Skan  tree initializer;
12794132718Skan
12795146895Skan  /* Assume the expression is constant.  */
12796146895Skan  *non_constant_p = false;
12797146895Skan
12798132718Skan  /* If it is not a `{', then we are looking at an
12799132718Skan     assignment-expression.  */
12800132718Skan  if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12801132718Skan    {
12802169689Skan      initializer
12803169689Skan	= cp_parser_constant_expression (parser,
12804169689Skan					/*allow_non_constant_p=*/true,
12805169689Skan					non_constant_p);
12806169689Skan      if (!*non_constant_p)
12807169689Skan	initializer = fold_non_dependent_expr (initializer);
12808132718Skan    }
12809132718Skan  else
12810132718Skan    {
12811132718Skan      /* Consume the `{' token.  */
12812132718Skan      cp_lexer_consume_token (parser->lexer);
12813132718Skan      /* Create a CONSTRUCTOR to represent the braced-initializer.  */
12814132718Skan      initializer = make_node (CONSTRUCTOR);
12815132718Skan      /* If it's not a `}', then there is a non-trivial initializer.  */
12816132718Skan      if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
12817132718Skan	{
12818132718Skan	  /* Parse the initializer list.  */
12819132718Skan	  CONSTRUCTOR_ELTS (initializer)
12820132718Skan	    = cp_parser_initializer_list (parser, non_constant_p);
12821132718Skan	  /* A trailing `,' token is allowed.  */
12822132718Skan	  if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12823132718Skan	    cp_lexer_consume_token (parser->lexer);
12824132718Skan	}
12825132718Skan      /* Now, there should be a trailing `}'.  */
12826132718Skan      cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12827132718Skan    }
12828132718Skan
12829132718Skan  return initializer;
12830132718Skan}
12831132718Skan
12832132718Skan/* Parse an initializer-list.
12833132718Skan
12834132718Skan   initializer-list:
12835132718Skan     initializer-clause
12836132718Skan     initializer-list , initializer-clause
12837132718Skan
12838132718Skan   GNU Extension:
12839169689Skan
12840132718Skan   initializer-list:
12841132718Skan     identifier : initializer-clause
12842132718Skan     initializer-list, identifier : initializer-clause
12843132718Skan
12844169689Skan   Returns a VEC of constructor_elt.  The VALUE of each elt is an expression
12845169689Skan   for the initializer.  If the INDEX of the elt is non-NULL, it is the
12846132718Skan   IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
12847132718Skan   as for cp_parser_initializer.  */
12848132718Skan
12849169689Skanstatic VEC(constructor_elt,gc) *
12850132718Skancp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
12851132718Skan{
12852169689Skan  VEC(constructor_elt,gc) *v = NULL;
12853132718Skan
12854132718Skan  /* Assume all of the expressions are constant.  */
12855132718Skan  *non_constant_p = false;
12856132718Skan
12857132718Skan  /* Parse the rest of the list.  */
12858132718Skan  while (true)
12859132718Skan    {
12860132718Skan      cp_token *token;
12861132718Skan      tree identifier;
12862132718Skan      tree initializer;
12863132718Skan      bool clause_non_constant_p;
12864132718Skan
12865132718Skan      /* If the next token is an identifier and the following one is a
12866132718Skan	 colon, we are looking at the GNU designated-initializer
12867132718Skan	 syntax.  */
12868132718Skan      if (cp_parser_allow_gnu_extensions_p (parser)
12869132718Skan	  && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
12870132718Skan	  && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
12871132718Skan	{
12872169689Skan	  /* Warn the user that they are using an extension.  */
12873169689Skan	  if (pedantic)
12874169689Skan	    pedwarn ("ISO C++ does not allow designated initializers");
12875132718Skan	  /* Consume the identifier.  */
12876169689Skan	  identifier = cp_lexer_consume_token (parser->lexer)->u.value;
12877132718Skan	  /* Consume the `:'.  */
12878132718Skan	  cp_lexer_consume_token (parser->lexer);
12879132718Skan	}
12880132718Skan      else
12881132718Skan	identifier = NULL_TREE;
12882132718Skan
12883132718Skan      /* Parse the initializer.  */
12884169689Skan      initializer = cp_parser_initializer_clause (parser,
12885132718Skan						  &clause_non_constant_p);
12886132718Skan      /* If any clause is non-constant, so is the entire initializer.  */
12887132718Skan      if (clause_non_constant_p)
12888132718Skan	*non_constant_p = true;
12889132718Skan
12890169689Skan      /* Add it to the vector.  */
12891169689Skan      CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
12892169689Skan
12893132718Skan      /* If the next token is not a comma, we have reached the end of
12894132718Skan	 the list.  */
12895132718Skan      if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12896132718Skan	break;
12897132718Skan
12898132718Skan      /* Peek at the next token.  */
12899132718Skan      token = cp_lexer_peek_nth_token (parser->lexer, 2);
12900132718Skan      /* If the next token is a `}', then we're still done.  An
12901132718Skan	 initializer-clause can have a trailing `,' after the
12902132718Skan	 initializer-list and before the closing `}'.  */
12903132718Skan      if (token->type == CPP_CLOSE_BRACE)
12904132718Skan	break;
12905132718Skan
12906132718Skan      /* Consume the `,' token.  */
12907132718Skan      cp_lexer_consume_token (parser->lexer);
12908132718Skan    }
12909132718Skan
12910169689Skan  return v;
12911132718Skan}
12912132718Skan
12913132718Skan/* Classes [gram.class] */
12914132718Skan
12915132718Skan/* Parse a class-name.
12916132718Skan
12917132718Skan   class-name:
12918132718Skan     identifier
12919132718Skan     template-id
12920132718Skan
12921132718Skan   TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
12922132718Skan   to indicate that names looked up in dependent types should be
12923132718Skan   assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
12924132718Skan   keyword has been used to indicate that the name that appears next
12925169689Skan   is a template.  TAG_TYPE indicates the explicit tag given before
12926169689Skan   the type name, if any.  If CHECK_DEPENDENCY_P is FALSE, names are
12927169689Skan   looked up in dependent scopes.  If CLASS_HEAD_P is TRUE, this class
12928169689Skan   is the class being defined in a class-head.
12929132718Skan
12930132718Skan   Returns the TYPE_DECL representing the class.  */
12931132718Skan
12932132718Skanstatic tree
12933169689Skancp_parser_class_name (cp_parser *parser,
12934169689Skan		      bool typename_keyword_p,
12935169689Skan		      bool template_keyword_p,
12936169689Skan		      enum tag_types tag_type,
12937132718Skan		      bool check_dependency_p,
12938132718Skan		      bool class_head_p,
12939132718Skan		      bool is_declaration)
12940132718Skan{
12941132718Skan  tree decl;
12942132718Skan  tree scope;
12943132718Skan  bool typename_p;
12944132718Skan  cp_token *token;
12945132718Skan
12946132718Skan  /* All class-names start with an identifier.  */
12947132718Skan  token = cp_lexer_peek_token (parser->lexer);
12948132718Skan  if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
12949132718Skan    {
12950132718Skan      cp_parser_error (parser, "expected class-name");
12951132718Skan      return error_mark_node;
12952132718Skan    }
12953169689Skan
12954132718Skan  /* PARSER->SCOPE can be cleared when parsing the template-arguments
12955132718Skan     to a template-id, so we save it here.  */
12956132718Skan  scope = parser->scope;
12957132718Skan  if (scope == error_mark_node)
12958132718Skan    return error_mark_node;
12959169689Skan
12960132718Skan  /* Any name names a type if we're following the `typename' keyword
12961132718Skan     in a qualified name where the enclosing scope is type-dependent.  */
12962132718Skan  typename_p = (typename_keyword_p && scope && TYPE_P (scope)
12963132718Skan		&& dependent_type_p (scope));
12964132718Skan  /* Handle the common case (an identifier, but not a template-id)
12965132718Skan     efficiently.  */
12966169689Skan  if (token->type == CPP_NAME
12967132718Skan      && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
12968132718Skan    {
12969169689Skan      cp_token *identifier_token;
12970132718Skan      tree identifier;
12971169689Skan      bool ambiguous_p;
12972132718Skan
12973132718Skan      /* Look for the identifier.  */
12974169689Skan      identifier_token = cp_lexer_peek_token (parser->lexer);
12975169689Skan      ambiguous_p = identifier_token->ambiguous_p;
12976132718Skan      identifier = cp_parser_identifier (parser);
12977132718Skan      /* If the next token isn't an identifier, we are certainly not
12978132718Skan	 looking at a class-name.  */
12979132718Skan      if (identifier == error_mark_node)
12980132718Skan	decl = error_mark_node;
12981132718Skan      /* If we know this is a type-name, there's no need to look it
12982132718Skan	 up.  */
12983132718Skan      else if (typename_p)
12984132718Skan	decl = identifier;
12985132718Skan      else
12986132718Skan	{
12987169689Skan	  tree ambiguous_decls;
12988169689Skan	  /* If we already know that this lookup is ambiguous, then
12989169689Skan	     we've already issued an error message; there's no reason
12990169689Skan	     to check again.  */
12991169689Skan	  if (ambiguous_p)
12992169689Skan	    {
12993169689Skan	      cp_parser_simulate_error (parser);
12994169689Skan	      return error_mark_node;
12995169689Skan	    }
12996132718Skan	  /* If the next token is a `::', then the name must be a type
12997132718Skan	     name.
12998132718Skan
12999132718Skan	     [basic.lookup.qual]
13000132718Skan
13001132718Skan	     During the lookup for a name preceding the :: scope
13002132718Skan	     resolution operator, object, function, and enumerator
13003132718Skan	     names are ignored.  */
13004132718Skan	  if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13005169689Skan	    tag_type = typename_type;
13006132718Skan	  /* Look up the name.  */
13007169689Skan	  decl = cp_parser_lookup_name (parser, identifier,
13008169689Skan					tag_type,
13009132718Skan					/*is_template=*/false,
13010132718Skan					/*is_namespace=*/false,
13011169689Skan					check_dependency_p,
13012169689Skan					&ambiguous_decls);
13013169689Skan	  if (ambiguous_decls)
13014169689Skan	    {
13015169689Skan	      error ("reference to %qD is ambiguous", identifier);
13016169689Skan	      print_candidates (ambiguous_decls);
13017169689Skan	      if (cp_parser_parsing_tentatively (parser))
13018169689Skan		{
13019169689Skan		  identifier_token->ambiguous_p = true;
13020169689Skan		  cp_parser_simulate_error (parser);
13021169689Skan		}
13022169689Skan	      return error_mark_node;
13023169689Skan	    }
13024132718Skan	}
13025132718Skan    }
13026132718Skan  else
13027132718Skan    {
13028132718Skan      /* Try a template-id.  */
13029132718Skan      decl = cp_parser_template_id (parser, template_keyword_p,
13030132718Skan				    check_dependency_p,
13031132718Skan				    is_declaration);
13032132718Skan      if (decl == error_mark_node)
13033132718Skan	return error_mark_node;
13034132718Skan    }
13035132718Skan
13036132718Skan  decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
13037132718Skan
13038132718Skan  /* If this is a typename, create a TYPENAME_TYPE.  */
13039132718Skan  if (typename_p && decl != error_mark_node)
13040132718Skan    {
13041169689Skan      decl = make_typename_type (scope, decl, typename_type,
13042169689Skan				 /*complain=*/tf_error);
13043132718Skan      if (decl != error_mark_node)
13044132718Skan	decl = TYPE_NAME (decl);
13045132718Skan    }
13046132718Skan
13047132718Skan  /* Check to see that it is really the name of a class.  */
13048169689Skan  if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
13049132718Skan      && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
13050132718Skan      && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13051132718Skan    /* Situations like this:
13052132718Skan
13053132718Skan	 template <typename T> struct A {
13054169689Skan	   typename T::template X<int>::I i;
13055132718Skan	 };
13056132718Skan
13057132718Skan       are problematic.  Is `T::template X<int>' a class-name?  The
13058132718Skan       standard does not seem to be definitive, but there is no other
13059132718Skan       valid interpretation of the following `::'.  Therefore, those
13060132718Skan       names are considered class-names.  */
13061132718Skan    {
13062169689Skan      decl = make_typename_type (scope, decl, tag_type, tf_error);
13063169689Skan      if (decl != error_mark_node)
13064169689Skan	decl = TYPE_NAME (decl);
13065132718Skan    }
13066169689Skan  else if (TREE_CODE (decl) != TYPE_DECL
13067169689Skan	   || TREE_TYPE (decl) == error_mark_node
13068169689Skan	   || !IS_AGGR_TYPE (TREE_TYPE (decl)))
13069169689Skan    decl = error_mark_node;
13070132718Skan
13071169689Skan  if (decl == error_mark_node)
13072169689Skan    cp_parser_error (parser, "expected class-name");
13073169689Skan
13074132718Skan  return decl;
13075132718Skan}
13076132718Skan
13077132718Skan/* Parse a class-specifier.
13078132718Skan
13079132718Skan   class-specifier:
13080132718Skan     class-head { member-specification [opt] }
13081132718Skan
13082132718Skan   Returns the TREE_TYPE representing the class.  */
13083132718Skan
13084132718Skanstatic tree
13085132718Skancp_parser_class_specifier (cp_parser* parser)
13086132718Skan{
13087132718Skan  cp_token *token;
13088132718Skan  tree type;
13089169689Skan  tree attributes = NULL_TREE;
13090132718Skan  int has_trailing_semicolon;
13091132718Skan  bool nested_name_specifier_p;
13092132718Skan  unsigned saved_num_template_parameter_lists;
13093169689Skan  bool saved_in_function_body;
13094169689Skan  tree old_scope = NULL_TREE;
13095146895Skan  tree scope = NULL_TREE;
13096169689Skan  tree bases;
13097132718Skan
13098132718Skan  push_deferring_access_checks (dk_no_deferred);
13099132718Skan
13100132718Skan  /* Parse the class-head.  */
13101132718Skan  type = cp_parser_class_head (parser,
13102132718Skan			       &nested_name_specifier_p,
13103169689Skan			       &attributes,
13104169689Skan			       &bases);
13105132718Skan  /* If the class-head was a semantic disaster, skip the entire body
13106132718Skan     of the class.  */
13107132718Skan  if (!type)
13108132718Skan    {
13109132718Skan      cp_parser_skip_to_end_of_block_or_statement (parser);
13110132718Skan      pop_deferring_access_checks ();
13111132718Skan      return error_mark_node;
13112132718Skan    }
13113132718Skan
13114132718Skan  /* Look for the `{'.  */
13115132718Skan  if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
13116132718Skan    {
13117132718Skan      pop_deferring_access_checks ();
13118132718Skan      return error_mark_node;
13119132718Skan    }
13120132718Skan
13121169689Skan  /* Process the base classes. If they're invalid, skip the
13122169689Skan     entire class body.  */
13123169689Skan  if (!xref_basetypes (type, bases))
13124169689Skan    {
13125169689Skan      cp_parser_skip_to_closing_brace (parser);
13126169689Skan
13127169689Skan      /* Consuming the closing brace yields better error messages
13128169689Skan         later on.  */
13129169689Skan      cp_lexer_consume_token (parser->lexer);
13130169689Skan      pop_deferring_access_checks ();
13131169689Skan      return error_mark_node;
13132169689Skan    }
13133169689Skan
13134132718Skan  /* Issue an error message if type-definitions are forbidden here.  */
13135132718Skan  cp_parser_check_type_definition (parser);
13136132718Skan  /* Remember that we are defining one more class.  */
13137132718Skan  ++parser->num_classes_being_defined;
13138132718Skan  /* Inside the class, surrounding template-parameter-lists do not
13139132718Skan     apply.  */
13140169689Skan  saved_num_template_parameter_lists
13141169689Skan    = parser->num_template_parameter_lists;
13142132718Skan  parser->num_template_parameter_lists = 0;
13143169689Skan  /* We are not in a function body.  */
13144169689Skan  saved_in_function_body = parser->in_function_body;
13145169689Skan  parser->in_function_body = false;
13146132718Skan
13147132718Skan  /* Start the class.  */
13148132718Skan  if (nested_name_specifier_p)
13149146895Skan    {
13150146895Skan      scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
13151169689Skan      old_scope = push_inner_scope (scope);
13152146895Skan    }
13153169689Skan  type = begin_class_definition (type, attributes);
13154169689Skan
13155132718Skan  if (type == error_mark_node)
13156132718Skan    /* If the type is erroneous, skip the entire body of the class.  */
13157132718Skan    cp_parser_skip_to_closing_brace (parser);
13158132718Skan  else
13159132718Skan    /* Parse the member-specification.  */
13160132718Skan    cp_parser_member_specification_opt (parser);
13161169689Skan
13162132718Skan  /* Look for the trailing `}'.  */
13163132718Skan  cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
13164132718Skan  /* We get better error messages by noticing a common problem: a
13165132718Skan     missing trailing `;'.  */
13166132718Skan  token = cp_lexer_peek_token (parser->lexer);
13167132718Skan  has_trailing_semicolon = (token->type == CPP_SEMICOLON);
13168132718Skan  /* Look for trailing attributes to apply to this class.  */
13169132718Skan  if (cp_parser_allow_gnu_extensions_p (parser))
13170169689Skan    attributes = cp_parser_attributes_opt (parser);
13171132718Skan  if (type != error_mark_node)
13172132718Skan    type = finish_struct (type, attributes);
13173169689Skan  if (nested_name_specifier_p)
13174169689Skan    pop_inner_scope (old_scope, scope);
13175132718Skan  /* If this class is not itself within the scope of another class,
13176132718Skan     then we need to parse the bodies of all of the queued function
13177132718Skan     definitions.  Note that the queued functions defined in a class
13178132718Skan     are not always processed immediately following the
13179132718Skan     class-specifier for that class.  Consider:
13180132718Skan
13181132718Skan       struct A {
13182169689Skan	 struct B { void f() { sizeof (A); } };
13183132718Skan       };
13184132718Skan
13185132718Skan     If `f' were processed before the processing of `A' were
13186132718Skan     completed, there would be no way to compute the size of `A'.
13187132718Skan     Note that the nesting we are interested in here is lexical --
13188132718Skan     not the semantic nesting given by TYPE_CONTEXT.  In particular,
13189132718Skan     for:
13190132718Skan
13191132718Skan       struct A { struct B; };
13192132718Skan       struct A::B { void f() { } };
13193132718Skan
13194132718Skan     there is no need to delay the parsing of `A::B::f'.  */
13195169689Skan  if (--parser->num_classes_being_defined == 0)
13196132718Skan    {
13197132718Skan      tree queue_entry;
13198132718Skan      tree fn;
13199169689Skan      tree class_type = NULL_TREE;
13200169689Skan      tree pushed_scope = NULL_TREE;
13201132718Skan
13202132718Skan      /* In a first pass, parse default arguments to the functions.
13203132718Skan	 Then, in a second pass, parse the bodies of the functions.
13204132718Skan	 This two-phased approach handles cases like:
13205132718Skan
13206169689Skan	    struct S {
13207169689Skan	      void f() { g(); }
13208169689Skan	      void g(int i = 3);
13209169689Skan	    };
13210169689Skan
13211169689Skan	 */
13212132718Skan      for (TREE_PURPOSE (parser->unparsed_functions_queues)
13213132718Skan	     = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
13214132718Skan	   (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
13215132718Skan	   TREE_PURPOSE (parser->unparsed_functions_queues)
13216132718Skan	     = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
13217132718Skan	{
13218132718Skan	  fn = TREE_VALUE (queue_entry);
13219169689Skan	  /* If there are default arguments that have not yet been processed,
13220169689Skan	     take care of them now.  */
13221169689Skan	  if (class_type != TREE_PURPOSE (queue_entry))
13222169689Skan	    {
13223169689Skan	      if (pushed_scope)
13224169689Skan		pop_scope (pushed_scope);
13225169689Skan	      class_type = TREE_PURPOSE (queue_entry);
13226169689Skan	      pushed_scope = push_scope (class_type);
13227169689Skan	    }
13228132718Skan	  /* Make sure that any template parameters are in scope.  */
13229132718Skan	  maybe_begin_member_template_processing (fn);
13230169689Skan	  /* Parse the default argument expressions.  */
13231132718Skan	  cp_parser_late_parsing_default_args (parser, fn);
13232132718Skan	  /* Remove any template parameters from the symbol table.  */
13233132718Skan	  maybe_end_member_template_processing ();
13234132718Skan	}
13235169689Skan      if (pushed_scope)
13236169689Skan	pop_scope (pushed_scope);
13237132718Skan      /* Now parse the body of the functions.  */
13238132718Skan      for (TREE_VALUE (parser->unparsed_functions_queues)
13239132718Skan	     = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
13240132718Skan	   (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
13241132718Skan	   TREE_VALUE (parser->unparsed_functions_queues)
13242132718Skan	     = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
13243132718Skan	{
13244132718Skan	  /* Figure out which function we need to process.  */
13245132718Skan	  fn = TREE_VALUE (queue_entry);
13246132718Skan	  /* Parse the function.  */
13247132718Skan	  cp_parser_late_parsing_for_member (parser, fn);
13248132718Skan	}
13249132718Skan    }
13250132718Skan
13251132718Skan  /* Put back any saved access checks.  */
13252132718Skan  pop_deferring_access_checks ();
13253132718Skan
13254169689Skan  /* Restore saved state.  */
13255169689Skan  parser->in_function_body = saved_in_function_body;
13256132718Skan  parser->num_template_parameter_lists
13257132718Skan    = saved_num_template_parameter_lists;
13258132718Skan
13259132718Skan  return type;
13260132718Skan}
13261132718Skan
13262132718Skan/* Parse a class-head.
13263132718Skan
13264132718Skan   class-head:
13265132718Skan     class-key identifier [opt] base-clause [opt]
13266132718Skan     class-key nested-name-specifier identifier base-clause [opt]
13267169689Skan     class-key nested-name-specifier [opt] template-id
13268169689Skan       base-clause [opt]
13269132718Skan
13270132718Skan   GNU Extensions:
13271132718Skan     class-key attributes identifier [opt] base-clause [opt]
13272132718Skan     class-key attributes nested-name-specifier identifier base-clause [opt]
13273169689Skan     class-key attributes nested-name-specifier [opt] template-id
13274169689Skan       base-clause [opt]
13275132718Skan
13276132718Skan   Returns the TYPE of the indicated class.  Sets
13277132718Skan   *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
13278132718Skan   involving a nested-name-specifier was used, and FALSE otherwise.
13279132718Skan
13280169689Skan   Returns error_mark_node if this is not a class-head.
13281169689Skan
13282132718Skan   Returns NULL_TREE if the class-head is syntactically valid, but
13283132718Skan   semantically invalid in a way that means we should skip the entire
13284132718Skan   body of the class.  */
13285132718Skan
13286132718Skanstatic tree
13287169689Skancp_parser_class_head (cp_parser* parser,
13288132718Skan		      bool* nested_name_specifier_p,
13289169689Skan		      tree *attributes_p,
13290169689Skan		      tree *bases)
13291132718Skan{
13292132718Skan  tree nested_name_specifier;
13293132718Skan  enum tag_types class_key;
13294132718Skan  tree id = NULL_TREE;
13295132718Skan  tree type = NULL_TREE;
13296132718Skan  tree attributes;
13297132718Skan  bool template_id_p = false;
13298132718Skan  bool qualified_p = false;
13299132718Skan  bool invalid_nested_name_p = false;
13300132718Skan  bool invalid_explicit_specialization_p = false;
13301169689Skan  tree pushed_scope = NULL_TREE;
13302132718Skan  unsigned num_templates;
13303132718Skan
13304132718Skan  /* Assume no nested-name-specifier will be present.  */
13305132718Skan  *nested_name_specifier_p = false;
13306132718Skan  /* Assume no template parameter lists will be used in defining the
13307132718Skan     type.  */
13308132718Skan  num_templates = 0;
13309132718Skan
13310132718Skan  /* Look for the class-key.  */
13311132718Skan  class_key = cp_parser_class_key (parser);
13312132718Skan  if (class_key == none_type)
13313132718Skan    return error_mark_node;
13314132718Skan
13315132718Skan  /* Parse the attributes.  */
13316132718Skan  attributes = cp_parser_attributes_opt (parser);
13317132718Skan
13318132718Skan  /* If the next token is `::', that is invalid -- but sometimes
13319132718Skan     people do try to write:
13320132718Skan
13321169689Skan       struct ::S {};
13322132718Skan
13323132718Skan     Handle this gracefully by accepting the extra qualifier, and then
13324132718Skan     issuing an error about it later if this really is a
13325132718Skan     class-head.  If it turns out just to be an elaborated type
13326132718Skan     specifier, remain silent.  */
13327132718Skan  if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
13328132718Skan    qualified_p = true;
13329132718Skan
13330132718Skan  push_deferring_access_checks (dk_no_check);
13331132718Skan
13332132718Skan  /* Determine the name of the class.  Begin by looking for an
13333132718Skan     optional nested-name-specifier.  */
13334169689Skan  nested_name_specifier
13335132718Skan    = cp_parser_nested_name_specifier_opt (parser,
13336132718Skan					   /*typename_keyword_p=*/false,
13337132718Skan					   /*check_dependency_p=*/false,
13338132718Skan					   /*type_p=*/false,
13339132718Skan					   /*is_declaration=*/false);
13340132718Skan  /* If there was a nested-name-specifier, then there *must* be an
13341132718Skan     identifier.  */
13342132718Skan  if (nested_name_specifier)
13343132718Skan    {
13344132718Skan      /* Although the grammar says `identifier', it really means
13345132718Skan	 `class-name' or `template-name'.  You are only allowed to
13346132718Skan	 define a class that has already been declared with this
13347169689Skan	 syntax.
13348132718Skan
13349169689Skan	 The proposed resolution for Core Issue 180 says that wherever
13350132718Skan	 you see `class T::X' you should treat `X' as a type-name.
13351169689Skan
13352132718Skan	 It is OK to define an inaccessible class; for example:
13353169689Skan
13354169689Skan	   class A { class B; };
13355169689Skan	   class A::B {};
13356169689Skan
13357169689Skan	 We do not know if we will see a class-name, or a
13358132718Skan	 template-name.  We look for a class-name first, in case the
13359132718Skan	 class-name is a template-id; if we looked for the
13360132718Skan	 template-name first we would stop after the template-name.  */
13361132718Skan      cp_parser_parse_tentatively (parser);
13362132718Skan      type = cp_parser_class_name (parser,
13363132718Skan				   /*typename_keyword_p=*/false,
13364132718Skan				   /*template_keyword_p=*/false,
13365169689Skan				   class_type,
13366132718Skan				   /*check_dependency_p=*/false,
13367132718Skan				   /*class_head_p=*/true,
13368132718Skan				   /*is_declaration=*/false);
13369132718Skan      /* If that didn't work, ignore the nested-name-specifier.  */
13370132718Skan      if (!cp_parser_parse_definitely (parser))
13371132718Skan	{
13372132718Skan	  invalid_nested_name_p = true;
13373132718Skan	  id = cp_parser_identifier (parser);
13374132718Skan	  if (id == error_mark_node)
13375132718Skan	    id = NULL_TREE;
13376132718Skan	}
13377132718Skan      /* If we could not find a corresponding TYPE, treat this
13378132718Skan	 declaration like an unqualified declaration.  */
13379132718Skan      if (type == error_mark_node)
13380132718Skan	nested_name_specifier = NULL_TREE;
13381132718Skan      /* Otherwise, count the number of templates used in TYPE and its
13382132718Skan	 containing scopes.  */
13383169689Skan      else
13384132718Skan	{
13385132718Skan	  tree scope;
13386132718Skan
13387169689Skan	  for (scope = TREE_TYPE (type);
13388132718Skan	       scope && TREE_CODE (scope) != NAMESPACE_DECL;
13389169689Skan	       scope = (TYPE_P (scope)
13390132718Skan			? TYPE_CONTEXT (scope)
13391169689Skan			: DECL_CONTEXT (scope)))
13392169689Skan	    if (TYPE_P (scope)
13393132718Skan		&& CLASS_TYPE_P (scope)
13394132718Skan		&& CLASSTYPE_TEMPLATE_INFO (scope)
13395132718Skan		&& PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
13396132718Skan		&& !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
13397132718Skan	      ++num_templates;
13398132718Skan	}
13399132718Skan    }
13400132718Skan  /* Otherwise, the identifier is optional.  */
13401132718Skan  else
13402132718Skan    {
13403132718Skan      /* We don't know whether what comes next is a template-id,
13404132718Skan	 an identifier, or nothing at all.  */
13405132718Skan      cp_parser_parse_tentatively (parser);
13406132718Skan      /* Check for a template-id.  */
13407169689Skan      id = cp_parser_template_id (parser,
13408132718Skan				  /*template_keyword_p=*/false,
13409132718Skan				  /*check_dependency_p=*/true,
13410132718Skan				  /*is_declaration=*/true);
13411132718Skan      /* If that didn't work, it could still be an identifier.  */
13412132718Skan      if (!cp_parser_parse_definitely (parser))
13413132718Skan	{
13414132718Skan	  if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13415132718Skan	    id = cp_parser_identifier (parser);
13416132718Skan	  else
13417132718Skan	    id = NULL_TREE;
13418132718Skan	}
13419132718Skan      else
13420132718Skan	{
13421132718Skan	  template_id_p = true;
13422132718Skan	  ++num_templates;
13423132718Skan	}
13424132718Skan    }
13425132718Skan
13426132718Skan  pop_deferring_access_checks ();
13427132718Skan
13428132718Skan  if (id)
13429132718Skan    cp_parser_check_for_invalid_template_id (parser, id);
13430132718Skan
13431132718Skan  /* If it's not a `:' or a `{' then we can't really be looking at a
13432132718Skan     class-head, since a class-head only appears as part of a
13433132718Skan     class-specifier.  We have to detect this situation before calling
13434132718Skan     xref_tag, since that has irreversible side-effects.  */
13435132718Skan  if (!cp_parser_next_token_starts_class_definition_p (parser))
13436132718Skan    {
13437169689Skan      cp_parser_error (parser, "expected %<{%> or %<:%>");
13438132718Skan      return error_mark_node;
13439132718Skan    }
13440132718Skan
13441132718Skan  /* At this point, we're going ahead with the class-specifier, even
13442132718Skan     if some other problem occurs.  */
13443132718Skan  cp_parser_commit_to_tentative_parse (parser);
13444132718Skan  /* Issue the error about the overly-qualified name now.  */
13445132718Skan  if (qualified_p)
13446132718Skan    cp_parser_error (parser,
13447132718Skan		     "global qualification of class name is invalid");
13448132718Skan  else if (invalid_nested_name_p)
13449132718Skan    cp_parser_error (parser,
13450132718Skan		     "qualified name does not name a class");
13451132718Skan  else if (nested_name_specifier)
13452132718Skan    {
13453132718Skan      tree scope;
13454146895Skan
13455146895Skan      /* Reject typedef-names in class heads.  */
13456146895Skan      if (!DECL_IMPLICIT_TYPEDEF_P (type))
13457146895Skan	{
13458169689Skan	  error ("invalid class name in declaration of %qD", type);
13459146895Skan	  type = NULL_TREE;
13460146895Skan	  goto done;
13461146895Skan	}
13462146895Skan
13463132718Skan      /* Figure out in what scope the declaration is being placed.  */
13464132718Skan      scope = current_scope ();
13465132718Skan      /* If that scope does not contain the scope in which the
13466132718Skan	 class was originally declared, the program is invalid.  */
13467132718Skan      if (scope && !is_ancestor (scope, nested_name_specifier))
13468132718Skan	{
13469169689Skan	  error ("declaration of %qD in %qD which does not enclose %qD",
13470169689Skan		 type, scope, nested_name_specifier);
13471132718Skan	  type = NULL_TREE;
13472132718Skan	  goto done;
13473132718Skan	}
13474132718Skan      /* [dcl.meaning]
13475132718Skan
13476169689Skan	 A declarator-id shall not be qualified exception of the
13477132718Skan	 definition of a ... nested class outside of its class
13478132718Skan	 ... [or] a the definition or explicit instantiation of a
13479132718Skan	 class member of a namespace outside of its namespace.  */
13480132718Skan      if (scope == nested_name_specifier)
13481132718Skan	{
13482132718Skan	  pedwarn ("extra qualification ignored");
13483132718Skan	  nested_name_specifier = NULL_TREE;
13484132718Skan	  num_templates = 0;
13485132718Skan	}
13486132718Skan    }
13487132718Skan  /* An explicit-specialization must be preceded by "template <>".  If
13488132718Skan     it is not, try to recover gracefully.  */
13489169689Skan  if (at_namespace_scope_p ()
13490132718Skan      && parser->num_template_parameter_lists == 0
13491132718Skan      && template_id_p)
13492132718Skan    {
13493169689Skan      error ("an explicit specialization must be preceded by %<template <>%>");
13494132718Skan      invalid_explicit_specialization_p = true;
13495132718Skan      /* Take the same action that would have been taken by
13496132718Skan	 cp_parser_explicit_specialization.  */
13497132718Skan      ++parser->num_template_parameter_lists;
13498132718Skan      begin_specialization ();
13499132718Skan    }
13500132718Skan  /* There must be no "return" statements between this point and the
13501132718Skan     end of this function; set "type "to the correct return value and
13502132718Skan     use "goto done;" to return.  */
13503132718Skan  /* Make sure that the right number of template parameters were
13504132718Skan     present.  */
13505132718Skan  if (!cp_parser_check_template_parameters (parser, num_templates))
13506132718Skan    {
13507132718Skan      /* If something went wrong, there is no point in even trying to
13508132718Skan	 process the class-definition.  */
13509132718Skan      type = NULL_TREE;
13510132718Skan      goto done;
13511132718Skan    }
13512132718Skan
13513132718Skan  /* Look up the type.  */
13514132718Skan  if (template_id_p)
13515132718Skan    {
13516132718Skan      type = TREE_TYPE (id);
13517169689Skan      type = maybe_process_partial_specialization (type);
13518169689Skan      if (nested_name_specifier)
13519169689Skan	pushed_scope = push_scope (nested_name_specifier);
13520132718Skan    }
13521169689Skan  else if (nested_name_specifier)
13522132718Skan    {
13523132718Skan      tree class_type;
13524132718Skan
13525132718Skan      /* Given:
13526132718Skan
13527132718Skan	    template <typename T> struct S { struct T };
13528132718Skan	    template <typename T> struct S<T>::T { };
13529132718Skan
13530132718Skan	 we will get a TYPENAME_TYPE when processing the definition of
13531132718Skan	 `S::T'.  We need to resolve it to the actual type before we
13532132718Skan	 try to define it.  */
13533132718Skan      if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
13534132718Skan	{
13535132718Skan	  class_type = resolve_typename_type (TREE_TYPE (type),
13536132718Skan					      /*only_current_p=*/false);
13537132718Skan	  if (class_type != error_mark_node)
13538132718Skan	    type = TYPE_NAME (class_type);
13539132718Skan	  else
13540132718Skan	    {
13541132718Skan	      cp_parser_error (parser, "could not resolve typename type");
13542132718Skan	      type = error_mark_node;
13543132718Skan	    }
13544132718Skan	}
13545132718Skan
13546132718Skan      maybe_process_partial_specialization (TREE_TYPE (type));
13547132718Skan      class_type = current_class_type;
13548132718Skan      /* Enter the scope indicated by the nested-name-specifier.  */
13549169689Skan      pushed_scope = push_scope (nested_name_specifier);
13550132718Skan      /* Get the canonical version of this type.  */
13551132718Skan      type = TYPE_MAIN_DECL (TREE_TYPE (type));
13552132718Skan      if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13553132718Skan	  && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
13554132718Skan	{
13555169689Skan	  type = push_template_decl (type);
13556169689Skan	  if (type == error_mark_node)
13557169689Skan	    {
13558169689Skan	      type = NULL_TREE;
13559169689Skan	      goto done;
13560169689Skan	    }
13561132718Skan	}
13562169689Skan
13563169689Skan      type = TREE_TYPE (type);
13564169689Skan      *nested_name_specifier_p = true;
13565132718Skan    }
13566169689Skan  else      /* The name is not a nested name.  */
13567169689Skan    {
13568169689Skan      /* If the class was unnamed, create a dummy name.  */
13569169689Skan      if (!id)
13570169689Skan	id = make_anon_name ();
13571169689Skan      type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
13572169689Skan		       parser->num_template_parameter_lists);
13573169689Skan    }
13574169689Skan
13575132718Skan  /* Indicate whether this class was declared as a `class' or as a
13576132718Skan     `struct'.  */
13577132718Skan  if (TREE_CODE (type) == RECORD_TYPE)
13578132718Skan    CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
13579132718Skan  cp_parser_check_class_key (class_key, type);
13580132718Skan
13581169689Skan  /* If this type was already complete, and we see another definition,
13582169689Skan     that's an error.  */
13583169689Skan  if (type != error_mark_node && COMPLETE_TYPE_P (type))
13584169689Skan    {
13585169689Skan      error ("redefinition of %q#T", type);
13586169689Skan      error ("previous definition of %q+#T", type);
13587169689Skan      type = NULL_TREE;
13588169689Skan      goto done;
13589169689Skan    }
13590169689Skan  else if (type == error_mark_node)
13591169689Skan    type = NULL_TREE;
13592132718Skan
13593169689Skan  /* We will have entered the scope containing the class; the names of
13594169689Skan     base classes should be looked up in that context.  For example:
13595169689Skan
13596132718Skan       struct A { struct B {}; struct C; };
13597132718Skan       struct A::C : B {};
13598132718Skan
13599132718Skan     is valid.  */
13600169689Skan  *bases = NULL_TREE;
13601132718Skan
13602169689Skan  /* Get the list of base-classes, if there is one.  */
13603169689Skan  if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13604169689Skan    *bases = cp_parser_base_clause (parser);
13605169689Skan
13606169689Skan done:
13607132718Skan  /* Leave the scope given by the nested-name-specifier.  We will
13608132718Skan     enter the class scope itself while processing the members.  */
13609169689Skan  if (pushed_scope)
13610169689Skan    pop_scope (pushed_scope);
13611132718Skan
13612132718Skan  if (invalid_explicit_specialization_p)
13613132718Skan    {
13614132718Skan      end_specialization ();
13615132718Skan      --parser->num_template_parameter_lists;
13616132718Skan    }
13617132718Skan  *attributes_p = attributes;
13618132718Skan  return type;
13619132718Skan}
13620132718Skan
13621132718Skan/* Parse a class-key.
13622132718Skan
13623132718Skan   class-key:
13624132718Skan     class
13625132718Skan     struct
13626132718Skan     union
13627132718Skan
13628132718Skan   Returns the kind of class-key specified, or none_type to indicate
13629132718Skan   error.  */
13630132718Skan
13631132718Skanstatic enum tag_types
13632132718Skancp_parser_class_key (cp_parser* parser)
13633132718Skan{
13634132718Skan  cp_token *token;
13635132718Skan  enum tag_types tag_type;
13636132718Skan
13637132718Skan  /* Look for the class-key.  */
13638132718Skan  token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
13639132718Skan  if (!token)
13640132718Skan    return none_type;
13641132718Skan
13642132718Skan  /* Check to see if the TOKEN is a class-key.  */
13643132718Skan  tag_type = cp_parser_token_is_class_key (token);
13644132718Skan  if (!tag_type)
13645132718Skan    cp_parser_error (parser, "expected class-key");
13646132718Skan  return tag_type;
13647132718Skan}
13648132718Skan
13649132718Skan/* Parse an (optional) member-specification.
13650132718Skan
13651132718Skan   member-specification:
13652132718Skan     member-declaration member-specification [opt]
13653132718Skan     access-specifier : member-specification [opt]  */
13654132718Skan
13655132718Skanstatic void
13656132718Skancp_parser_member_specification_opt (cp_parser* parser)
13657132718Skan{
13658132718Skan  while (true)
13659132718Skan    {
13660132718Skan      cp_token *token;
13661132718Skan      enum rid keyword;
13662132718Skan
13663132718Skan      /* Peek at the next token.  */
13664132718Skan      token = cp_lexer_peek_token (parser->lexer);
13665132718Skan      /* If it's a `}', or EOF then we've seen all the members.  */
13666169689Skan      if (token->type == CPP_CLOSE_BRACE
13667169689Skan	  || token->type == CPP_EOF
13668169689Skan	  || token->type == CPP_PRAGMA_EOL)
13669132718Skan	break;
13670132718Skan
13671132718Skan      /* See if this token is a keyword.  */
13672132718Skan      keyword = token->keyword;
13673132718Skan      switch (keyword)
13674132718Skan	{
13675132718Skan	case RID_PUBLIC:
13676132718Skan	case RID_PROTECTED:
13677132718Skan	case RID_PRIVATE:
13678132718Skan	  /* Consume the access-specifier.  */
13679132718Skan	  cp_lexer_consume_token (parser->lexer);
13680132718Skan	  /* Remember which access-specifier is active.  */
13681169689Skan	  current_access_specifier = token->u.value;
13682132718Skan	  /* Look for the `:'.  */
13683132718Skan	  cp_parser_require (parser, CPP_COLON, "`:'");
13684132718Skan	  break;
13685132718Skan
13686132718Skan	default:
13687169689Skan	  /* Accept #pragmas at class scope.  */
13688169689Skan	  if (token->type == CPP_PRAGMA)
13689169689Skan	    {
13690169689Skan	      cp_parser_pragma (parser, pragma_external);
13691169689Skan	      break;
13692169689Skan	    }
13693169689Skan
13694132718Skan	  /* Otherwise, the next construction must be a
13695132718Skan	     member-declaration.  */
13696132718Skan	  cp_parser_member_declaration (parser);
13697132718Skan	}
13698132718Skan    }
13699132718Skan}
13700132718Skan
13701169689Skan/* Parse a member-declaration.
13702132718Skan
13703132718Skan   member-declaration:
13704132718Skan     decl-specifier-seq [opt] member-declarator-list [opt] ;
13705132718Skan     function-definition ; [opt]
13706132718Skan     :: [opt] nested-name-specifier template [opt] unqualified-id ;
13707132718Skan     using-declaration
13708169689Skan     template-declaration
13709132718Skan
13710132718Skan   member-declarator-list:
13711132718Skan     member-declarator
13712132718Skan     member-declarator-list , member-declarator
13713132718Skan
13714132718Skan   member-declarator:
13715169689Skan     declarator pure-specifier [opt]
13716132718Skan     declarator constant-initializer [opt]
13717169689Skan     identifier [opt] : constant-expression
13718132718Skan
13719132718Skan   GNU Extensions:
13720132718Skan
13721132718Skan   member-declaration:
13722132718Skan     __extension__ member-declaration
13723132718Skan
13724132718Skan   member-declarator:
13725132718Skan     declarator attributes [opt] pure-specifier [opt]
13726132718Skan     declarator attributes [opt] constant-initializer [opt]
13727132718Skan     identifier [opt] attributes [opt] : constant-expression  */
13728132718Skan
13729132718Skanstatic void
13730132718Skancp_parser_member_declaration (cp_parser* parser)
13731132718Skan{
13732169689Skan  cp_decl_specifier_seq decl_specifiers;
13733132718Skan  tree prefix_attributes;
13734132718Skan  tree decl;
13735132718Skan  int declares_class_or_enum;
13736132718Skan  bool friend_p;
13737132718Skan  cp_token *token;
13738132718Skan  int saved_pedantic;
13739132718Skan
13740132718Skan  /* Check for the `__extension__' keyword.  */
13741132718Skan  if (cp_parser_extension_opt (parser, &saved_pedantic))
13742132718Skan    {
13743132718Skan      /* Recurse.  */
13744132718Skan      cp_parser_member_declaration (parser);
13745132718Skan      /* Restore the old value of the PEDANTIC flag.  */
13746132718Skan      pedantic = saved_pedantic;
13747132718Skan
13748132718Skan      return;
13749132718Skan    }
13750132718Skan
13751132718Skan  /* Check for a template-declaration.  */
13752132718Skan  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
13753132718Skan    {
13754161651Skan      /* An explicit specialization here is an error condition, and we
13755161651Skan	 expect the specialization handler to detect and report this.  */
13756161651Skan      if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
13757161651Skan	  && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
13758161651Skan	cp_parser_explicit_specialization (parser);
13759161651Skan      else
13760161651Skan	cp_parser_template_declaration (parser, /*member_p=*/true);
13761132718Skan
13762132718Skan      return;
13763132718Skan    }
13764132718Skan
13765132718Skan  /* Check for a using-declaration.  */
13766132718Skan  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
13767132718Skan    {
13768132718Skan      /* Parse the using-declaration.  */
13769169689Skan      cp_parser_using_declaration (parser,
13770169689Skan				   /*access_declaration_p=*/false);
13771169689Skan      return;
13772169689Skan    }
13773132718Skan
13774169689Skan  /* Check for @defs.  */
13775169689Skan  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
13776169689Skan    {
13777169689Skan      tree ivar, member;
13778169689Skan      tree ivar_chains = cp_parser_objc_defs_expression (parser);
13779169689Skan      ivar = ivar_chains;
13780169689Skan      while (ivar)
13781169689Skan	{
13782169689Skan	  member = ivar;
13783169689Skan	  ivar = TREE_CHAIN (member);
13784169689Skan	  TREE_CHAIN (member) = NULL_TREE;
13785169689Skan	  finish_member_declaration (member);
13786169689Skan	}
13787132718Skan      return;
13788132718Skan    }
13789169689Skan
13790169689Skan  if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
13791169689Skan    return;
13792169689Skan
13793132718Skan  /* Parse the decl-specifier-seq.  */
13794169689Skan  cp_parser_decl_specifier_seq (parser,
13795169689Skan				CP_PARSER_FLAGS_OPTIONAL,
13796169689Skan				&decl_specifiers,
13797169689Skan				&declares_class_or_enum);
13798169689Skan  prefix_attributes = decl_specifiers.attributes;
13799169689Skan  decl_specifiers.attributes = NULL_TREE;
13800132718Skan  /* Check for an invalid type-name.  */
13801169689Skan  if (!decl_specifiers.type
13802169689Skan      && cp_parser_parse_and_diagnose_invalid_type_name (parser))
13803132718Skan    return;
13804132718Skan  /* If there is no declarator, then the decl-specifier-seq should
13805132718Skan     specify a type.  */
13806132718Skan  if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13807132718Skan    {
13808132718Skan      /* If there was no decl-specifier-seq, and the next token is a
13809132718Skan	 `;', then we have something like:
13810132718Skan
13811132718Skan	   struct S { ; };
13812132718Skan
13813132718Skan	 [class.mem]
13814132718Skan
13815132718Skan	 Each member-declaration shall declare at least one member
13816132718Skan	 name of the class.  */
13817169689Skan      if (!decl_specifiers.any_specifiers_p)
13818132718Skan	{
13819169689Skan	  cp_token *token = cp_lexer_peek_token (parser->lexer);
13820169689Skan	  if (pedantic && !token->in_system_header)
13821169689Skan	    pedwarn ("%Hextra %<;%>", &token->location);
13822132718Skan	}
13823169689Skan      else
13824132718Skan	{
13825132718Skan	  tree type;
13826169689Skan
13827132718Skan	  /* See if this declaration is a friend.  */
13828169689Skan	  friend_p = cp_parser_friend_p (&decl_specifiers);
13829132718Skan	  /* If there were decl-specifiers, check to see if there was
13830132718Skan	     a class-declaration.  */
13831169689Skan	  type = check_tag_decl (&decl_specifiers);
13832132718Skan	  /* Nested classes have already been added to the class, but
13833132718Skan	     a `friend' needs to be explicitly registered.  */
13834132718Skan	  if (friend_p)
13835132718Skan	    {
13836132718Skan	      /* If the `friend' keyword was present, the friend must
13837132718Skan		 be introduced with a class-key.  */
13838132718Skan	       if (!declares_class_or_enum)
13839132718Skan		 error ("a class-key must be used when declaring a friend");
13840132718Skan	       /* In this case:
13841132718Skan
13842169689Skan		    template <typename T> struct A {
13843169689Skan		      friend struct A<T>::B;
13844169689Skan		    };
13845169689Skan
13846132718Skan		  A<T>::B will be represented by a TYPENAME_TYPE, and
13847132718Skan		  therefore not recognized by check_tag_decl.  */
13848169689Skan	       if (!type
13849169689Skan		   && decl_specifiers.type
13850169689Skan		   && TYPE_P (decl_specifiers.type))
13851169689Skan		 type = decl_specifiers.type;
13852132718Skan	       if (!type || !TYPE_P (type))
13853132718Skan		 error ("friend declaration does not name a class or "
13854132718Skan			"function");
13855132718Skan	       else
13856132718Skan		 make_friend_class (current_class_type, type,
13857132718Skan				    /*complain=*/true);
13858132718Skan	    }
13859132718Skan	  /* If there is no TYPE, an error message will already have
13860132718Skan	     been issued.  */
13861169689Skan	  else if (!type || type == error_mark_node)
13862132718Skan	    ;
13863132718Skan	  /* An anonymous aggregate has to be handled specially; such
13864132718Skan	     a declaration really declares a data member (with a
13865132718Skan	     particular type), as opposed to a nested class.  */
13866132718Skan	  else if (ANON_AGGR_TYPE_P (type))
13867132718Skan	    {
13868132718Skan	      /* Remove constructors and such from TYPE, now that we
13869132718Skan		 know it is an anonymous aggregate.  */
13870132718Skan	      fixup_anonymous_aggr (type);
13871132718Skan	      /* And make the corresponding data member.  */
13872132718Skan	      decl = build_decl (FIELD_DECL, NULL_TREE, type);
13873132718Skan	      /* Add it to the class.  */
13874132718Skan	      finish_member_declaration (decl);
13875132718Skan	    }
13876132718Skan	  else
13877132718Skan	    cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
13878132718Skan	}
13879132718Skan    }
13880132718Skan  else
13881132718Skan    {
13882132718Skan      /* See if these declarations will be friends.  */
13883169689Skan      friend_p = cp_parser_friend_p (&decl_specifiers);
13884132718Skan
13885169689Skan      /* Keep going until we hit the `;' at the end of the
13886132718Skan	 declaration.  */
13887132718Skan      while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13888132718Skan	{
13889132718Skan	  tree attributes = NULL_TREE;
13890132718Skan	  tree first_attribute;
13891132718Skan
13892132718Skan	  /* Peek at the next token.  */
13893132718Skan	  token = cp_lexer_peek_token (parser->lexer);
13894132718Skan
13895132718Skan	  /* Check for a bitfield declaration.  */
13896132718Skan	  if (token->type == CPP_COLON
13897132718Skan	      || (token->type == CPP_NAME
13898169689Skan		  && cp_lexer_peek_nth_token (parser->lexer, 2)->type
13899132718Skan		  == CPP_COLON))
13900132718Skan	    {
13901132718Skan	      tree identifier;
13902132718Skan	      tree width;
13903132718Skan
13904132718Skan	      /* Get the name of the bitfield.  Note that we cannot just
13905132718Skan		 check TOKEN here because it may have been invalidated by
13906132718Skan		 the call to cp_lexer_peek_nth_token above.  */
13907132718Skan	      if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
13908132718Skan		identifier = cp_parser_identifier (parser);
13909132718Skan	      else
13910132718Skan		identifier = NULL_TREE;
13911132718Skan
13912132718Skan	      /* Consume the `:' token.  */
13913132718Skan	      cp_lexer_consume_token (parser->lexer);
13914132718Skan	      /* Get the width of the bitfield.  */
13915169689Skan	      width
13916132718Skan		= cp_parser_constant_expression (parser,
13917132718Skan						 /*allow_non_constant=*/false,
13918132718Skan						 NULL);
13919132718Skan
13920132718Skan	      /* Look for attributes that apply to the bitfield.  */
13921132718Skan	      attributes = cp_parser_attributes_opt (parser);
13922132718Skan	      /* Remember which attributes are prefix attributes and
13923132718Skan		 which are not.  */
13924132718Skan	      first_attribute = attributes;
13925132718Skan	      /* Combine the attributes.  */
13926132718Skan	      attributes = chainon (prefix_attributes, attributes);
13927132718Skan
13928132718Skan	      /* Create the bitfield declaration.  */
13929169689Skan	      decl = grokbitfield (identifier
13930169689Skan				   ? make_id_declarator (NULL_TREE,
13931169689Skan							 identifier,
13932169689Skan							 sfk_none)
13933169689Skan				   : NULL,
13934169689Skan				   &decl_specifiers,
13935132718Skan				   width);
13936132718Skan	      /* Apply the attributes.  */
13937132718Skan	      cplus_decl_attributes (&decl, attributes, /*flags=*/0);
13938132718Skan	    }
13939132718Skan	  else
13940132718Skan	    {
13941169689Skan	      cp_declarator *declarator;
13942132718Skan	      tree initializer;
13943132718Skan	      tree asm_specification;
13944132718Skan	      int ctor_dtor_or_conv_p;
13945132718Skan
13946132718Skan	      /* Parse the declarator.  */
13947169689Skan	      declarator
13948132718Skan		= cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13949132718Skan					&ctor_dtor_or_conv_p,
13950146895Skan					/*parenthesized_p=*/NULL,
13951146895Skan					/*member_p=*/true);
13952132718Skan
13953132718Skan	      /* If something went wrong parsing the declarator, make sure
13954132718Skan		 that we at least consume some tokens.  */
13955169689Skan	      if (declarator == cp_error_declarator)
13956132718Skan		{
13957132718Skan		  /* Skip to the end of the statement.  */
13958132718Skan		  cp_parser_skip_to_end_of_statement (parser);
13959132718Skan		  /* If the next token is not a semicolon, that is
13960132718Skan		     probably because we just skipped over the body of
13961132718Skan		     a function.  So, we consume a semicolon if
13962132718Skan		     present, but do not issue an error message if it
13963132718Skan		     is not present.  */
13964132718Skan		  if (cp_lexer_next_token_is (parser->lexer,
13965132718Skan					      CPP_SEMICOLON))
13966132718Skan		    cp_lexer_consume_token (parser->lexer);
13967132718Skan		  return;
13968132718Skan		}
13969132718Skan
13970161651Skan	      if (declares_class_or_enum & 2)
13971161651Skan		cp_parser_check_for_definition_in_return_type
13972169689Skan		  (declarator, decl_specifiers.type);
13973132718Skan
13974132718Skan	      /* Look for an asm-specification.  */
13975132718Skan	      asm_specification = cp_parser_asm_specification_opt (parser);
13976132718Skan	      /* Look for attributes that apply to the declaration.  */
13977132718Skan	      attributes = cp_parser_attributes_opt (parser);
13978132718Skan	      /* Remember which attributes are prefix attributes and
13979132718Skan		 which are not.  */
13980132718Skan	      first_attribute = attributes;
13981132718Skan	      /* Combine the attributes.  */
13982132718Skan	      attributes = chainon (prefix_attributes, attributes);
13983132718Skan
13984132718Skan	      /* If it's an `=', then we have a constant-initializer or a
13985132718Skan		 pure-specifier.  It is not correct to parse the
13986132718Skan		 initializer before registering the member declaration
13987132718Skan		 since the member declaration should be in scope while
13988132718Skan		 its initializer is processed.  However, the rest of the
13989132718Skan		 front end does not yet provide an interface that allows
13990132718Skan		 us to handle this correctly.  */
13991132718Skan	      if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13992132718Skan		{
13993132718Skan		  /* In [class.mem]:
13994132718Skan
13995132718Skan		     A pure-specifier shall be used only in the declaration of
13996169689Skan		     a virtual function.
13997132718Skan
13998132718Skan		     A member-declarator can contain a constant-initializer
13999132718Skan		     only if it declares a static member of integral or
14000169689Skan		     enumeration type.
14001132718Skan
14002132718Skan		     Therefore, if the DECLARATOR is for a function, we look
14003132718Skan		     for a pure-specifier; otherwise, we look for a
14004132718Skan		     constant-initializer.  When we call `grokfield', it will
14005132718Skan		     perform more stringent semantics checks.  */
14006169689Skan		  if (function_declarator_p (declarator))
14007132718Skan		    initializer = cp_parser_pure_specifier (parser);
14008132718Skan		  else
14009132718Skan		    /* Parse the initializer.  */
14010132718Skan		    initializer = cp_parser_constant_initializer (parser);
14011132718Skan		}
14012132718Skan	      /* Otherwise, there is no initializer.  */
14013132718Skan	      else
14014132718Skan		initializer = NULL_TREE;
14015132718Skan
14016132718Skan	      /* See if we are probably looking at a function
14017169689Skan		 definition.  We are certainly not looking at a
14018132718Skan		 member-declarator.  Calling `grokfield' has
14019132718Skan		 side-effects, so we must not do it unless we are sure
14020132718Skan		 that we are looking at a member-declarator.  */
14021169689Skan	      if (cp_parser_token_starts_function_definition_p
14022132718Skan		  (cp_lexer_peek_token (parser->lexer)))
14023132718Skan		{
14024132718Skan		  /* The grammar does not allow a pure-specifier to be
14025132718Skan		     used when a member function is defined.  (It is
14026132718Skan		     possible that this fact is an oversight in the
14027132718Skan		     standard, since a pure function may be defined
14028132718Skan		     outside of the class-specifier.  */
14029132718Skan		  if (initializer)
14030132718Skan		    error ("pure-specifier on function-definition");
14031132718Skan		  decl = cp_parser_save_member_function_body (parser,
14032169689Skan							      &decl_specifiers,
14033132718Skan							      declarator,
14034132718Skan							      attributes);
14035132718Skan		  /* If the member was not a friend, declare it here.  */
14036132718Skan		  if (!friend_p)
14037132718Skan		    finish_member_declaration (decl);
14038132718Skan		  /* Peek at the next token.  */
14039132718Skan		  token = cp_lexer_peek_token (parser->lexer);
14040132718Skan		  /* If the next token is a semicolon, consume it.  */
14041132718Skan		  if (token->type == CPP_SEMICOLON)
14042132718Skan		    cp_lexer_consume_token (parser->lexer);
14043132718Skan		  return;
14044132718Skan		}
14045132718Skan	      else
14046169689Skan		/* Create the declaration.  */
14047169689Skan		decl = grokfield (declarator, &decl_specifiers,
14048169689Skan				  initializer, /*init_const_expr_p=*/true,
14049169689Skan				  asm_specification,
14050169689Skan				  attributes);
14051132718Skan	    }
14052132718Skan
14053132718Skan	  /* Reset PREFIX_ATTRIBUTES.  */
14054132718Skan	  while (attributes && TREE_CHAIN (attributes) != first_attribute)
14055132718Skan	    attributes = TREE_CHAIN (attributes);
14056132718Skan	  if (attributes)
14057132718Skan	    TREE_CHAIN (attributes) = NULL_TREE;
14058132718Skan
14059132718Skan	  /* If there is any qualification still in effect, clear it
14060132718Skan	     now; we will be starting fresh with the next declarator.  */
14061132718Skan	  parser->scope = NULL_TREE;
14062132718Skan	  parser->qualifying_scope = NULL_TREE;
14063132718Skan	  parser->object_scope = NULL_TREE;
14064132718Skan	  /* If it's a `,', then there are more declarators.  */
14065132718Skan	  if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14066132718Skan	    cp_lexer_consume_token (parser->lexer);
14067132718Skan	  /* If the next token isn't a `;', then we have a parse error.  */
14068132718Skan	  else if (cp_lexer_next_token_is_not (parser->lexer,
14069132718Skan					       CPP_SEMICOLON))
14070132718Skan	    {
14071169689Skan	      cp_parser_error (parser, "expected %<;%>");
14072132718Skan	      /* Skip tokens until we find a `;'.  */
14073132718Skan	      cp_parser_skip_to_end_of_statement (parser);
14074132718Skan
14075132718Skan	      break;
14076132718Skan	    }
14077132718Skan
14078132718Skan	  if (decl)
14079132718Skan	    {
14080132718Skan	      /* Add DECL to the list of members.  */
14081132718Skan	      if (!friend_p)
14082132718Skan		finish_member_declaration (decl);
14083132718Skan
14084132718Skan	      if (TREE_CODE (decl) == FUNCTION_DECL)
14085132718Skan		cp_parser_save_default_args (parser, decl);
14086132718Skan	    }
14087132718Skan	}
14088132718Skan    }
14089132718Skan
14090132718Skan  cp_parser_require (parser, CPP_SEMICOLON, "`;'");
14091132718Skan}
14092132718Skan
14093132718Skan/* Parse a pure-specifier.
14094132718Skan
14095132718Skan   pure-specifier:
14096132718Skan     = 0
14097132718Skan
14098132718Skan   Returns INTEGER_ZERO_NODE if a pure specifier is found.
14099132718Skan   Otherwise, ERROR_MARK_NODE is returned.  */
14100132718Skan
14101132718Skanstatic tree
14102132718Skancp_parser_pure_specifier (cp_parser* parser)
14103132718Skan{
14104132718Skan  cp_token *token;
14105132718Skan
14106132718Skan  /* Look for the `=' token.  */
14107132718Skan  if (!cp_parser_require (parser, CPP_EQ, "`='"))
14108132718Skan    return error_mark_node;
14109132718Skan  /* Look for the `0' token.  */
14110169689Skan  token = cp_lexer_consume_token (parser->lexer);
14111169689Skan  /* c_lex_with_flags marks a single digit '0' with PURE_ZERO.  */
14112169689Skan  if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
14113169689Skan    {
14114169689Skan      cp_parser_error (parser,
14115169689Skan		       "invalid pure specifier (only `= 0' is allowed)");
14116169689Skan      cp_parser_skip_to_end_of_statement (parser);
14117169689Skan      return error_mark_node;
14118169689Skan    }
14119169689Skan  if (PROCESSING_REAL_TEMPLATE_DECL_P ())
14120169689Skan    {
14121169689Skan      error ("templates may not be %<virtual%>");
14122169689Skan      return error_mark_node;
14123169689Skan    }
14124132718Skan
14125132718Skan  return integer_zero_node;
14126132718Skan}
14127132718Skan
14128132718Skan/* Parse a constant-initializer.
14129132718Skan
14130132718Skan   constant-initializer:
14131132718Skan     = constant-expression
14132132718Skan
14133132718Skan   Returns a representation of the constant-expression.  */
14134132718Skan
14135132718Skanstatic tree
14136132718Skancp_parser_constant_initializer (cp_parser* parser)
14137132718Skan{
14138132718Skan  /* Look for the `=' token.  */
14139132718Skan  if (!cp_parser_require (parser, CPP_EQ, "`='"))
14140132718Skan    return error_mark_node;
14141132718Skan
14142132718Skan  /* It is invalid to write:
14143132718Skan
14144132718Skan       struct S { static const int i = { 7 }; };
14145132718Skan
14146132718Skan     */
14147132718Skan  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14148132718Skan    {
14149132718Skan      cp_parser_error (parser,
14150132718Skan		       "a brace-enclosed initializer is not allowed here");
14151132718Skan      /* Consume the opening brace.  */
14152132718Skan      cp_lexer_consume_token (parser->lexer);
14153132718Skan      /* Skip the initializer.  */
14154132718Skan      cp_parser_skip_to_closing_brace (parser);
14155132718Skan      /* Look for the trailing `}'.  */
14156132718Skan      cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
14157169689Skan
14158132718Skan      return error_mark_node;
14159132718Skan    }
14160132718Skan
14161169689Skan  return cp_parser_constant_expression (parser,
14162132718Skan					/*allow_non_constant=*/false,
14163132718Skan					NULL);
14164132718Skan}
14165132718Skan
14166132718Skan/* Derived classes [gram.class.derived] */
14167132718Skan
14168132718Skan/* Parse a base-clause.
14169132718Skan
14170132718Skan   base-clause:
14171169689Skan     : base-specifier-list
14172132718Skan
14173132718Skan   base-specifier-list:
14174132718Skan     base-specifier
14175132718Skan     base-specifier-list , base-specifier
14176132718Skan
14177132718Skan   Returns a TREE_LIST representing the base-classes, in the order in
14178132718Skan   which they were declared.  The representation of each node is as
14179169689Skan   described by cp_parser_base_specifier.
14180132718Skan
14181132718Skan   In the case that no bases are specified, this function will return
14182132718Skan   NULL_TREE, not ERROR_MARK_NODE.  */
14183132718Skan
14184132718Skanstatic tree
14185132718Skancp_parser_base_clause (cp_parser* parser)
14186132718Skan{
14187132718Skan  tree bases = NULL_TREE;
14188132718Skan
14189132718Skan  /* Look for the `:' that begins the list.  */
14190132718Skan  cp_parser_require (parser, CPP_COLON, "`:'");
14191132718Skan
14192132718Skan  /* Scan the base-specifier-list.  */
14193132718Skan  while (true)
14194132718Skan    {
14195132718Skan      cp_token *token;
14196132718Skan      tree base;
14197132718Skan
14198132718Skan      /* Look for the base-specifier.  */
14199132718Skan      base = cp_parser_base_specifier (parser);
14200132718Skan      /* Add BASE to the front of the list.  */
14201132718Skan      if (base != error_mark_node)
14202132718Skan	{
14203132718Skan	  TREE_CHAIN (base) = bases;
14204132718Skan	  bases = base;
14205132718Skan	}
14206132718Skan      /* Peek at the next token.  */
14207132718Skan      token = cp_lexer_peek_token (parser->lexer);
14208132718Skan      /* If it's not a comma, then the list is complete.  */
14209132718Skan      if (token->type != CPP_COMMA)
14210132718Skan	break;
14211132718Skan      /* Consume the `,'.  */
14212132718Skan      cp_lexer_consume_token (parser->lexer);
14213132718Skan    }
14214132718Skan
14215132718Skan  /* PARSER->SCOPE may still be non-NULL at this point, if the last
14216132718Skan     base class had a qualified name.  However, the next name that
14217132718Skan     appears is certainly not qualified.  */
14218132718Skan  parser->scope = NULL_TREE;
14219132718Skan  parser->qualifying_scope = NULL_TREE;
14220132718Skan  parser->object_scope = NULL_TREE;
14221132718Skan
14222132718Skan  return nreverse (bases);
14223132718Skan}
14224132718Skan
14225132718Skan/* Parse a base-specifier.
14226132718Skan
14227132718Skan   base-specifier:
14228132718Skan     :: [opt] nested-name-specifier [opt] class-name
14229132718Skan     virtual access-specifier [opt] :: [opt] nested-name-specifier
14230132718Skan       [opt] class-name
14231132718Skan     access-specifier virtual [opt] :: [opt] nested-name-specifier
14232132718Skan       [opt] class-name
14233132718Skan
14234132718Skan   Returns a TREE_LIST.  The TREE_PURPOSE will be one of
14235132718Skan   ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
14236132718Skan   indicate the specifiers provided.  The TREE_VALUE will be a TYPE
14237132718Skan   (or the ERROR_MARK_NODE) indicating the type that was specified.  */
14238169689Skan
14239132718Skanstatic tree
14240132718Skancp_parser_base_specifier (cp_parser* parser)
14241132718Skan{
14242132718Skan  cp_token *token;
14243132718Skan  bool done = false;
14244132718Skan  bool virtual_p = false;
14245132718Skan  bool duplicate_virtual_error_issued_p = false;
14246132718Skan  bool duplicate_access_error_issued_p = false;
14247132718Skan  bool class_scope_p, template_p;
14248132718Skan  tree access = access_default_node;
14249132718Skan  tree type;
14250132718Skan
14251132718Skan  /* Process the optional `virtual' and `access-specifier'.  */
14252132718Skan  while (!done)
14253132718Skan    {
14254132718Skan      /* Peek at the next token.  */
14255132718Skan      token = cp_lexer_peek_token (parser->lexer);
14256132718Skan      /* Process `virtual'.  */
14257132718Skan      switch (token->keyword)
14258132718Skan	{
14259132718Skan	case RID_VIRTUAL:
14260132718Skan	  /* If `virtual' appears more than once, issue an error.  */
14261132718Skan	  if (virtual_p && !duplicate_virtual_error_issued_p)
14262132718Skan	    {
14263132718Skan	      cp_parser_error (parser,
14264169689Skan			       "%<virtual%> specified more than once in base-specified");
14265132718Skan	      duplicate_virtual_error_issued_p = true;
14266132718Skan	    }
14267132718Skan
14268132718Skan	  virtual_p = true;
14269132718Skan
14270132718Skan	  /* Consume the `virtual' token.  */
14271132718Skan	  cp_lexer_consume_token (parser->lexer);
14272132718Skan
14273132718Skan	  break;
14274132718Skan
14275132718Skan	case RID_PUBLIC:
14276132718Skan	case RID_PROTECTED:
14277132718Skan	case RID_PRIVATE:
14278132718Skan	  /* If more than one access specifier appears, issue an
14279132718Skan	     error.  */
14280132718Skan	  if (access != access_default_node
14281132718Skan	      && !duplicate_access_error_issued_p)
14282132718Skan	    {
14283132718Skan	      cp_parser_error (parser,
14284132718Skan			       "more than one access specifier in base-specified");
14285132718Skan	      duplicate_access_error_issued_p = true;
14286132718Skan	    }
14287132718Skan
14288132718Skan	  access = ridpointers[(int) token->keyword];
14289132718Skan
14290132718Skan	  /* Consume the access-specifier.  */
14291132718Skan	  cp_lexer_consume_token (parser->lexer);
14292132718Skan
14293132718Skan	  break;
14294132718Skan
14295132718Skan	default:
14296132718Skan	  done = true;
14297132718Skan	  break;
14298132718Skan	}
14299132718Skan    }
14300169689Skan  /* It is not uncommon to see programs mechanically, erroneously, use
14301132718Skan     the 'typename' keyword to denote (dependent) qualified types
14302132718Skan     as base classes.  */
14303132718Skan  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
14304132718Skan    {
14305132718Skan      if (!processing_template_decl)
14306169689Skan	error ("keyword %<typename%> not allowed outside of templates");
14307132718Skan      else
14308169689Skan	error ("keyword %<typename%> not allowed in this context "
14309132718Skan	       "(the base class is implicitly a type)");
14310132718Skan      cp_lexer_consume_token (parser->lexer);
14311132718Skan    }
14312132718Skan
14313132718Skan  /* Look for the optional `::' operator.  */
14314132718Skan  cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
14315132718Skan  /* Look for the nested-name-specifier.  The simplest way to
14316132718Skan     implement:
14317132718Skan
14318132718Skan       [temp.res]
14319132718Skan
14320132718Skan       The keyword `typename' is not permitted in a base-specifier or
14321132718Skan       mem-initializer; in these contexts a qualified name that
14322132718Skan       depends on a template-parameter is implicitly assumed to be a
14323132718Skan       type name.
14324132718Skan
14325132718Skan     is to pretend that we have seen the `typename' keyword at this
14326169689Skan     point.  */
14327132718Skan  cp_parser_nested_name_specifier_opt (parser,
14328132718Skan				       /*typename_keyword_p=*/true,
14329132718Skan				       /*check_dependency_p=*/true,
14330169689Skan				       typename_type,
14331132718Skan				       /*is_declaration=*/true);
14332132718Skan  /* If the base class is given by a qualified name, assume that names
14333132718Skan     we see are type names or templates, as appropriate.  */
14334132718Skan  class_scope_p = (parser->scope && TYPE_P (parser->scope));
14335132718Skan  template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
14336169689Skan
14337132718Skan  /* Finally, look for the class-name.  */
14338169689Skan  type = cp_parser_class_name (parser,
14339132718Skan			       class_scope_p,
14340132718Skan			       template_p,
14341169689Skan			       typename_type,
14342132718Skan			       /*check_dependency_p=*/true,
14343132718Skan			       /*class_head_p=*/false,
14344132718Skan			       /*is_declaration=*/true);
14345132718Skan
14346132718Skan  if (type == error_mark_node)
14347132718Skan    return error_mark_node;
14348132718Skan
14349132718Skan  return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
14350132718Skan}
14351132718Skan
14352132718Skan/* Exception handling [gram.exception] */
14353132718Skan
14354132718Skan/* Parse an (optional) exception-specification.
14355132718Skan
14356132718Skan   exception-specification:
14357132718Skan     throw ( type-id-list [opt] )
14358132718Skan
14359132718Skan   Returns a TREE_LIST representing the exception-specification.  The
14360132718Skan   TREE_VALUE of each node is a type.  */
14361132718Skan
14362132718Skanstatic tree
14363132718Skancp_parser_exception_specification_opt (cp_parser* parser)
14364132718Skan{
14365132718Skan  cp_token *token;
14366132718Skan  tree type_id_list;
14367132718Skan
14368132718Skan  /* Peek at the next token.  */
14369132718Skan  token = cp_lexer_peek_token (parser->lexer);
14370132718Skan  /* If it's not `throw', then there's no exception-specification.  */
14371132718Skan  if (!cp_parser_is_keyword (token, RID_THROW))
14372132718Skan    return NULL_TREE;
14373132718Skan
14374132718Skan  /* Consume the `throw'.  */
14375132718Skan  cp_lexer_consume_token (parser->lexer);
14376132718Skan
14377132718Skan  /* Look for the `('.  */
14378132718Skan  cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14379132718Skan
14380132718Skan  /* Peek at the next token.  */
14381132718Skan  token = cp_lexer_peek_token (parser->lexer);
14382132718Skan  /* If it's not a `)', then there is a type-id-list.  */
14383132718Skan  if (token->type != CPP_CLOSE_PAREN)
14384132718Skan    {
14385132718Skan      const char *saved_message;
14386132718Skan
14387132718Skan      /* Types may not be defined in an exception-specification.  */
14388132718Skan      saved_message = parser->type_definition_forbidden_message;
14389132718Skan      parser->type_definition_forbidden_message
14390132718Skan	= "types may not be defined in an exception-specification";
14391132718Skan      /* Parse the type-id-list.  */
14392132718Skan      type_id_list = cp_parser_type_id_list (parser);
14393132718Skan      /* Restore the saved message.  */
14394132718Skan      parser->type_definition_forbidden_message = saved_message;
14395132718Skan    }
14396132718Skan  else
14397132718Skan    type_id_list = empty_except_spec;
14398132718Skan
14399132718Skan  /* Look for the `)'.  */
14400132718Skan  cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14401132718Skan
14402132718Skan  return type_id_list;
14403132718Skan}
14404132718Skan
14405132718Skan/* Parse an (optional) type-id-list.
14406132718Skan
14407132718Skan   type-id-list:
14408132718Skan     type-id
14409132718Skan     type-id-list , type-id
14410132718Skan
14411132718Skan   Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
14412132718Skan   in the order that the types were presented.  */
14413132718Skan
14414132718Skanstatic tree
14415132718Skancp_parser_type_id_list (cp_parser* parser)
14416132718Skan{
14417132718Skan  tree types = NULL_TREE;
14418132718Skan
14419132718Skan  while (true)
14420132718Skan    {
14421132718Skan      cp_token *token;
14422132718Skan      tree type;
14423132718Skan
14424132718Skan      /* Get the next type-id.  */
14425132718Skan      type = cp_parser_type_id (parser);
14426132718Skan      /* Add it to the list.  */
14427132718Skan      types = add_exception_specifier (types, type, /*complain=*/1);
14428132718Skan      /* Peek at the next token.  */
14429132718Skan      token = cp_lexer_peek_token (parser->lexer);
14430132718Skan      /* If it is not a `,', we are done.  */
14431132718Skan      if (token->type != CPP_COMMA)
14432132718Skan	break;
14433132718Skan      /* Consume the `,'.  */
14434132718Skan      cp_lexer_consume_token (parser->lexer);
14435132718Skan    }
14436132718Skan
14437132718Skan  return nreverse (types);
14438132718Skan}
14439132718Skan
14440132718Skan/* Parse a try-block.
14441132718Skan
14442132718Skan   try-block:
14443132718Skan     try compound-statement handler-seq  */
14444132718Skan
14445132718Skanstatic tree
14446132718Skancp_parser_try_block (cp_parser* parser)
14447132718Skan{
14448132718Skan  tree try_block;
14449132718Skan
14450132718Skan  cp_parser_require_keyword (parser, RID_TRY, "`try'");
14451132718Skan  try_block = begin_try_block ();
14452169689Skan  cp_parser_compound_statement (parser, NULL, true);
14453132718Skan  finish_try_block (try_block);
14454132718Skan  cp_parser_handler_seq (parser);
14455132718Skan  finish_handler_sequence (try_block);
14456132718Skan
14457132718Skan  return try_block;
14458132718Skan}
14459132718Skan
14460132718Skan/* Parse a function-try-block.
14461132718Skan
14462132718Skan   function-try-block:
14463132718Skan     try ctor-initializer [opt] function-body handler-seq  */
14464132718Skan
14465132718Skanstatic bool
14466132718Skancp_parser_function_try_block (cp_parser* parser)
14467132718Skan{
14468169689Skan  tree compound_stmt;
14469132718Skan  tree try_block;
14470132718Skan  bool ctor_initializer_p;
14471132718Skan
14472132718Skan  /* Look for the `try' keyword.  */
14473132718Skan  if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
14474132718Skan    return false;
14475132718Skan  /* Let the rest of the front-end know where we are.  */
14476169689Skan  try_block = begin_function_try_block (&compound_stmt);
14477132718Skan  /* Parse the function-body.  */
14478169689Skan  ctor_initializer_p
14479132718Skan    = cp_parser_ctor_initializer_opt_and_function_body (parser);
14480132718Skan  /* We're done with the `try' part.  */
14481132718Skan  finish_function_try_block (try_block);
14482132718Skan  /* Parse the handlers.  */
14483132718Skan  cp_parser_handler_seq (parser);
14484132718Skan  /* We're done with the handlers.  */
14485169689Skan  finish_function_handler_sequence (try_block, compound_stmt);
14486132718Skan
14487132718Skan  return ctor_initializer_p;
14488132718Skan}
14489132718Skan
14490132718Skan/* Parse a handler-seq.
14491132718Skan
14492132718Skan   handler-seq:
14493132718Skan     handler handler-seq [opt]  */
14494132718Skan
14495132718Skanstatic void
14496132718Skancp_parser_handler_seq (cp_parser* parser)
14497132718Skan{
14498132718Skan  while (true)
14499132718Skan    {
14500132718Skan      cp_token *token;
14501132718Skan
14502132718Skan      /* Parse the handler.  */
14503132718Skan      cp_parser_handler (parser);
14504132718Skan      /* Peek at the next token.  */
14505132718Skan      token = cp_lexer_peek_token (parser->lexer);
14506132718Skan      /* If it's not `catch' then there are no more handlers.  */
14507132718Skan      if (!cp_parser_is_keyword (token, RID_CATCH))
14508132718Skan	break;
14509132718Skan    }
14510132718Skan}
14511132718Skan
14512132718Skan/* Parse a handler.
14513132718Skan
14514132718Skan   handler:
14515132718Skan     catch ( exception-declaration ) compound-statement  */
14516132718Skan
14517132718Skanstatic void
14518132718Skancp_parser_handler (cp_parser* parser)
14519132718Skan{
14520132718Skan  tree handler;
14521132718Skan  tree declaration;
14522132718Skan
14523132718Skan  cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
14524132718Skan  handler = begin_handler ();
14525132718Skan  cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14526132718Skan  declaration = cp_parser_exception_declaration (parser);
14527132718Skan  finish_handler_parms (declaration, handler);
14528132718Skan  cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14529169689Skan  cp_parser_compound_statement (parser, NULL, false);
14530132718Skan  finish_handler (handler);
14531132718Skan}
14532132718Skan
14533132718Skan/* Parse an exception-declaration.
14534132718Skan
14535132718Skan   exception-declaration:
14536132718Skan     type-specifier-seq declarator
14537132718Skan     type-specifier-seq abstract-declarator
14538132718Skan     type-specifier-seq
14539169689Skan     ...
14540132718Skan
14541132718Skan   Returns a VAR_DECL for the declaration, or NULL_TREE if the
14542132718Skan   ellipsis variant is used.  */
14543132718Skan
14544132718Skanstatic tree
14545132718Skancp_parser_exception_declaration (cp_parser* parser)
14546132718Skan{
14547169689Skan  cp_decl_specifier_seq type_specifiers;
14548169689Skan  cp_declarator *declarator;
14549132718Skan  const char *saved_message;
14550132718Skan
14551132718Skan  /* If it's an ellipsis, it's easy to handle.  */
14552132718Skan  if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14553132718Skan    {
14554132718Skan      /* Consume the `...' token.  */
14555132718Skan      cp_lexer_consume_token (parser->lexer);
14556132718Skan      return NULL_TREE;
14557132718Skan    }
14558132718Skan
14559132718Skan  /* Types may not be defined in exception-declarations.  */
14560132718Skan  saved_message = parser->type_definition_forbidden_message;
14561132718Skan  parser->type_definition_forbidden_message
14562132718Skan    = "types may not be defined in exception-declarations";
14563132718Skan
14564132718Skan  /* Parse the type-specifier-seq.  */
14565169689Skan  cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
14566169689Skan				&type_specifiers);
14567132718Skan  /* If it's a `)', then there is no declarator.  */
14568132718Skan  if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
14569169689Skan    declarator = NULL;
14570132718Skan  else
14571132718Skan    declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
14572132718Skan				       /*ctor_dtor_or_conv_p=*/NULL,
14573146895Skan				       /*parenthesized_p=*/NULL,
14574146895Skan				       /*member_p=*/false);
14575132718Skan
14576132718Skan  /* Restore the saved message.  */
14577132718Skan  parser->type_definition_forbidden_message = saved_message;
14578132718Skan
14579169689Skan  if (!type_specifiers.any_specifiers_p)
14580169689Skan    return error_mark_node;
14581169689Skan
14582169689Skan  return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
14583132718Skan}
14584132718Skan
14585169689Skan/* Parse a throw-expression.
14586132718Skan
14587132718Skan   throw-expression:
14588132718Skan     throw assignment-expression [opt]
14589132718Skan
14590132718Skan   Returns a THROW_EXPR representing the throw-expression.  */
14591132718Skan
14592132718Skanstatic tree
14593132718Skancp_parser_throw_expression (cp_parser* parser)
14594132718Skan{
14595132718Skan  tree expression;
14596132718Skan  cp_token* token;
14597132718Skan
14598132718Skan  cp_parser_require_keyword (parser, RID_THROW, "`throw'");
14599132718Skan  token = cp_lexer_peek_token (parser->lexer);
14600132718Skan  /* Figure out whether or not there is an assignment-expression
14601132718Skan     following the "throw" keyword.  */
14602132718Skan  if (token->type == CPP_COMMA
14603132718Skan      || token->type == CPP_SEMICOLON
14604132718Skan      || token->type == CPP_CLOSE_PAREN
14605132718Skan      || token->type == CPP_CLOSE_SQUARE
14606132718Skan      || token->type == CPP_CLOSE_BRACE
14607132718Skan      || token->type == CPP_COLON)
14608132718Skan    expression = NULL_TREE;
14609132718Skan  else
14610169689Skan    expression = cp_parser_assignment_expression (parser,
14611169689Skan						  /*cast_p=*/false);
14612132718Skan
14613132718Skan  return build_throw (expression);
14614132718Skan}
14615132718Skan
14616132718Skan/* GNU Extensions */
14617132718Skan
14618132718Skan/* Parse an (optional) asm-specification.
14619132718Skan
14620132718Skan   asm-specification:
14621132718Skan     asm ( string-literal )
14622132718Skan
14623132718Skan   If the asm-specification is present, returns a STRING_CST
14624132718Skan   corresponding to the string-literal.  Otherwise, returns
14625132718Skan   NULL_TREE.  */
14626132718Skan
14627132718Skanstatic tree
14628132718Skancp_parser_asm_specification_opt (cp_parser* parser)
14629132718Skan{
14630132718Skan  cp_token *token;
14631132718Skan  tree asm_specification;
14632132718Skan
14633132718Skan  /* Peek at the next token.  */
14634132718Skan  token = cp_lexer_peek_token (parser->lexer);
14635169689Skan  /* If the next token isn't the `asm' keyword, then there's no
14636132718Skan     asm-specification.  */
14637132718Skan  if (!cp_parser_is_keyword (token, RID_ASM))
14638132718Skan    return NULL_TREE;
14639132718Skan
14640132718Skan  /* Consume the `asm' token.  */
14641132718Skan  cp_lexer_consume_token (parser->lexer);
14642132718Skan  /* Look for the `('.  */
14643132718Skan  cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14644132718Skan
14645132718Skan  /* Look for the string-literal.  */
14646169689Skan  asm_specification = cp_parser_string_literal (parser, false, false);
14647132718Skan
14648132718Skan  /* Look for the `)'.  */
14649132718Skan  cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
14650132718Skan
14651132718Skan  return asm_specification;
14652132718Skan}
14653132718Skan
14654169689Skan/* Parse an asm-operand-list.
14655132718Skan
14656132718Skan   asm-operand-list:
14657132718Skan     asm-operand
14658132718Skan     asm-operand-list , asm-operand
14659169689Skan
14660132718Skan   asm-operand:
14661169689Skan     string-literal ( expression )
14662132718Skan     [ string-literal ] string-literal ( expression )
14663132718Skan
14664132718Skan   Returns a TREE_LIST representing the operands.  The TREE_VALUE of
14665132718Skan   each node is the expression.  The TREE_PURPOSE is itself a
14666132718Skan   TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
14667132718Skan   string-literal (or NULL_TREE if not present) and whose TREE_VALUE
14668132718Skan   is a STRING_CST for the string literal before the parenthesis.  */
14669132718Skan
14670132718Skanstatic tree
14671132718Skancp_parser_asm_operand_list (cp_parser* parser)
14672132718Skan{
14673132718Skan  tree asm_operands = NULL_TREE;
14674132718Skan
14675132718Skan  while (true)
14676132718Skan    {
14677132718Skan      tree string_literal;
14678132718Skan      tree expression;
14679132718Skan      tree name;
14680169689Skan
14681169689Skan      if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
14682132718Skan	{
14683132718Skan	  /* Consume the `[' token.  */
14684132718Skan	  cp_lexer_consume_token (parser->lexer);
14685132718Skan	  /* Read the operand name.  */
14686132718Skan	  name = cp_parser_identifier (parser);
14687169689Skan	  if (name != error_mark_node)
14688132718Skan	    name = build_string (IDENTIFIER_LENGTH (name),
14689132718Skan				 IDENTIFIER_POINTER (name));
14690132718Skan	  /* Look for the closing `]'.  */
14691132718Skan	  cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
14692132718Skan	}
14693132718Skan      else
14694132718Skan	name = NULL_TREE;
14695132718Skan      /* Look for the string-literal.  */
14696169689Skan      string_literal = cp_parser_string_literal (parser, false, false);
14697169689Skan
14698132718Skan      /* Look for the `('.  */
14699132718Skan      cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14700132718Skan      /* Parse the expression.  */
14701169689Skan      expression = cp_parser_expression (parser, /*cast_p=*/false);
14702132718Skan      /* Look for the `)'.  */
14703132718Skan      cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14704169689Skan
14705132718Skan      /* Add this operand to the list.  */
14706132718Skan      asm_operands = tree_cons (build_tree_list (name, string_literal),
14707169689Skan				expression,
14708132718Skan				asm_operands);
14709169689Skan      /* If the next token is not a `,', there are no more
14710132718Skan	 operands.  */
14711132718Skan      if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14712132718Skan	break;
14713132718Skan      /* Consume the `,'.  */
14714132718Skan      cp_lexer_consume_token (parser->lexer);
14715132718Skan    }
14716132718Skan
14717132718Skan  return nreverse (asm_operands);
14718132718Skan}
14719132718Skan
14720169689Skan/* Parse an asm-clobber-list.
14721132718Skan
14722132718Skan   asm-clobber-list:
14723132718Skan     string-literal
14724169689Skan     asm-clobber-list , string-literal
14725132718Skan
14726132718Skan   Returns a TREE_LIST, indicating the clobbers in the order that they
14727132718Skan   appeared.  The TREE_VALUE of each node is a STRING_CST.  */
14728132718Skan
14729132718Skanstatic tree
14730132718Skancp_parser_asm_clobber_list (cp_parser* parser)
14731132718Skan{
14732132718Skan  tree clobbers = NULL_TREE;
14733132718Skan
14734132718Skan  while (true)
14735132718Skan    {
14736132718Skan      tree string_literal;
14737132718Skan
14738132718Skan      /* Look for the string literal.  */
14739169689Skan      string_literal = cp_parser_string_literal (parser, false, false);
14740132718Skan      /* Add it to the list.  */
14741132718Skan      clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
14742169689Skan      /* If the next token is not a `,', then the list is
14743132718Skan	 complete.  */
14744132718Skan      if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14745132718Skan	break;
14746132718Skan      /* Consume the `,' token.  */
14747132718Skan      cp_lexer_consume_token (parser->lexer);
14748132718Skan    }
14749132718Skan
14750132718Skan  return clobbers;
14751132718Skan}
14752132718Skan
14753132718Skan/* Parse an (optional) series of attributes.
14754132718Skan
14755132718Skan   attributes:
14756132718Skan     attributes attribute
14757132718Skan
14758132718Skan   attribute:
14759169689Skan     __attribute__ (( attribute-list [opt] ))
14760132718Skan
14761132718Skan   The return value is as for cp_parser_attribute_list.  */
14762169689Skan
14763132718Skanstatic tree
14764132718Skancp_parser_attributes_opt (cp_parser* parser)
14765132718Skan{
14766132718Skan  tree attributes = NULL_TREE;
14767132718Skan
14768132718Skan  while (true)
14769132718Skan    {
14770132718Skan      cp_token *token;
14771132718Skan      tree attribute_list;
14772132718Skan
14773132718Skan      /* Peek at the next token.  */
14774132718Skan      token = cp_lexer_peek_token (parser->lexer);
14775132718Skan      /* If it's not `__attribute__', then we're done.  */
14776132718Skan      if (token->keyword != RID_ATTRIBUTE)
14777132718Skan	break;
14778132718Skan
14779132718Skan      /* Consume the `__attribute__' keyword.  */
14780132718Skan      cp_lexer_consume_token (parser->lexer);
14781132718Skan      /* Look for the two `(' tokens.  */
14782132718Skan      cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14783132718Skan      cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14784132718Skan
14785132718Skan      /* Peek at the next token.  */
14786132718Skan      token = cp_lexer_peek_token (parser->lexer);
14787132718Skan      if (token->type != CPP_CLOSE_PAREN)
14788132718Skan	/* Parse the attribute-list.  */
14789132718Skan	attribute_list = cp_parser_attribute_list (parser);
14790132718Skan      else
14791132718Skan	/* If the next token is a `)', then there is no attribute
14792132718Skan	   list.  */
14793132718Skan	attribute_list = NULL;
14794132718Skan
14795132718Skan      /* Look for the two `)' tokens.  */
14796132718Skan      cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14797132718Skan      cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14798132718Skan
14799132718Skan      /* Add these new attributes to the list.  */
14800132718Skan      attributes = chainon (attributes, attribute_list);
14801132718Skan    }
14802132718Skan
14803132718Skan  return attributes;
14804132718Skan}
14805132718Skan
14806169689Skan/* Parse an attribute-list.
14807132718Skan
14808169689Skan   attribute-list:
14809169689Skan     attribute
14810132718Skan     attribute-list , attribute
14811132718Skan
14812132718Skan   attribute:
14813169689Skan     identifier
14814132718Skan     identifier ( identifier )
14815132718Skan     identifier ( identifier , expression-list )
14816169689Skan     identifier ( expression-list )
14817132718Skan
14818169689Skan   Returns a TREE_LIST, or NULL_TREE on error.  Each node corresponds
14819169689Skan   to an attribute.  The TREE_PURPOSE of each node is the identifier
14820169689Skan   indicating which attribute is in use.  The TREE_VALUE represents
14821169689Skan   the arguments, if any.  */
14822132718Skan
14823132718Skanstatic tree
14824132718Skancp_parser_attribute_list (cp_parser* parser)
14825132718Skan{
14826132718Skan  tree attribute_list = NULL_TREE;
14827169689Skan  bool save_translate_strings_p = parser->translate_strings_p;
14828132718Skan
14829169689Skan  parser->translate_strings_p = false;
14830132718Skan  while (true)
14831132718Skan    {
14832132718Skan      cp_token *token;
14833132718Skan      tree identifier;
14834132718Skan      tree attribute;
14835132718Skan
14836132718Skan      /* Look for the identifier.  We also allow keywords here; for
14837132718Skan	 example `__attribute__ ((const))' is legal.  */
14838132718Skan      token = cp_lexer_peek_token (parser->lexer);
14839169689Skan      if (token->type == CPP_NAME
14840146895Skan	  || token->type == CPP_KEYWORD)
14841132718Skan	{
14842169689Skan	  tree arguments = NULL_TREE;
14843169689Skan
14844146895Skan	  /* Consume the token.  */
14845146895Skan	  token = cp_lexer_consume_token (parser->lexer);
14846132718Skan
14847169689Skan	  /* Save away the identifier that indicates which attribute
14848146895Skan	     this is.  */
14849169689Skan	  identifier = token->u.value;
14850146895Skan	  attribute = build_tree_list (identifier, NULL_TREE);
14851132718Skan
14852146895Skan	  /* Peek at the next token.  */
14853146895Skan	  token = cp_lexer_peek_token (parser->lexer);
14854146895Skan	  /* If it's an `(', then parse the attribute arguments.  */
14855146895Skan	  if (token->type == CPP_OPEN_PAREN)
14856146895Skan	    {
14857169689Skan	      arguments = cp_parser_parenthesized_expression_list
14858169689Skan			  (parser, true, /*cast_p=*/false,
14859169689Skan			   /*non_constant_p=*/NULL);
14860169689Skan	      /* Save the arguments away.  */
14861146895Skan	      TREE_VALUE (attribute) = arguments;
14862146895Skan	    }
14863146895Skan
14864169689Skan	  if (arguments != error_mark_node)
14865169689Skan	    {
14866169689Skan	      /* Add this attribute to the list.  */
14867169689Skan	      TREE_CHAIN (attribute) = attribute_list;
14868169689Skan	      attribute_list = attribute;
14869169689Skan	    }
14870146895Skan
14871146895Skan	  token = cp_lexer_peek_token (parser->lexer);
14872146895Skan	}
14873146895Skan      /* Now, look for more attributes.  If the next token isn't a
14874146895Skan	 `,', we're done.  */
14875132718Skan      if (token->type != CPP_COMMA)
14876132718Skan	break;
14877132718Skan
14878132718Skan      /* Consume the comma and keep going.  */
14879132718Skan      cp_lexer_consume_token (parser->lexer);
14880132718Skan    }
14881169689Skan  parser->translate_strings_p = save_translate_strings_p;
14882132718Skan
14883132718Skan  /* We built up the list in reverse order.  */
14884132718Skan  return nreverse (attribute_list);
14885132718Skan}
14886132718Skan
14887132718Skan/* Parse an optional `__extension__' keyword.  Returns TRUE if it is
14888132718Skan   present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
14889132718Skan   current value of the PEDANTIC flag, regardless of whether or not
14890132718Skan   the `__extension__' keyword is present.  The caller is responsible
14891132718Skan   for restoring the value of the PEDANTIC flag.  */
14892132718Skan
14893132718Skanstatic bool
14894132718Skancp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
14895132718Skan{
14896132718Skan  /* Save the old value of the PEDANTIC flag.  */
14897132718Skan  *saved_pedantic = pedantic;
14898132718Skan
14899132718Skan  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
14900132718Skan    {
14901132718Skan      /* Consume the `__extension__' token.  */
14902132718Skan      cp_lexer_consume_token (parser->lexer);
14903132718Skan      /* We're not being pedantic while the `__extension__' keyword is
14904132718Skan	 in effect.  */
14905132718Skan      pedantic = 0;
14906132718Skan
14907132718Skan      return true;
14908132718Skan    }
14909132718Skan
14910132718Skan  return false;
14911132718Skan}
14912132718Skan
14913132718Skan/* Parse a label declaration.
14914132718Skan
14915132718Skan   label-declaration:
14916132718Skan     __label__ label-declarator-seq ;
14917132718Skan
14918132718Skan   label-declarator-seq:
14919132718Skan     identifier , label-declarator-seq
14920132718Skan     identifier  */
14921132718Skan
14922132718Skanstatic void
14923132718Skancp_parser_label_declaration (cp_parser* parser)
14924132718Skan{
14925132718Skan  /* Look for the `__label__' keyword.  */
14926132718Skan  cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
14927132718Skan
14928132718Skan  while (true)
14929132718Skan    {
14930132718Skan      tree identifier;
14931132718Skan
14932132718Skan      /* Look for an identifier.  */
14933132718Skan      identifier = cp_parser_identifier (parser);
14934161651Skan      /* If we failed, stop.  */
14935161651Skan      if (identifier == error_mark_node)
14936161651Skan	break;
14937161651Skan      /* Declare it as a label.  */
14938132718Skan      finish_label_decl (identifier);
14939132718Skan      /* If the next token is a `;', stop.  */
14940132718Skan      if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14941132718Skan	break;
14942132718Skan      /* Look for the `,' separating the label declarations.  */
14943132718Skan      cp_parser_require (parser, CPP_COMMA, "`,'");
14944132718Skan    }
14945132718Skan
14946132718Skan  /* Look for the final `;'.  */
14947132718Skan  cp_parser_require (parser, CPP_SEMICOLON, "`;'");
14948132718Skan}
14949132718Skan
14950132718Skan/* Support Functions */
14951132718Skan
14952132718Skan/* Looks up NAME in the current scope, as given by PARSER->SCOPE.
14953132718Skan   NAME should have one of the representations used for an
14954132718Skan   id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
14955132718Skan   is returned.  If PARSER->SCOPE is a dependent type, then a
14956132718Skan   SCOPE_REF is returned.
14957132718Skan
14958132718Skan   If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
14959132718Skan   returned; the name was already resolved when the TEMPLATE_ID_EXPR
14960132718Skan   was formed.  Abstractly, such entities should not be passed to this
14961132718Skan   function, because they do not need to be looked up, but it is
14962132718Skan   simpler to check for this special case here, rather than at the
14963132718Skan   call-sites.
14964132718Skan
14965132718Skan   In cases not explicitly covered above, this function returns a
14966132718Skan   DECL, OVERLOAD, or baselink representing the result of the lookup.
14967132718Skan   If there was no entity with the indicated NAME, the ERROR_MARK_NODE
14968132718Skan   is returned.
14969132718Skan
14970169689Skan   If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
14971169689Skan   (e.g., "struct") that was used.  In that case bindings that do not
14972169689Skan   refer to types are ignored.
14973132718Skan
14974132718Skan   If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
14975132718Skan   ignored.
14976132718Skan
14977132718Skan   If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
14978132718Skan   are ignored.
14979132718Skan
14980132718Skan   If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
14981169689Skan   types.
14982132718Skan
14983169689Skan   If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
14984169689Skan   TREE_LIST of candidates if name-lookup results in an ambiguity, and
14985169689Skan   NULL_TREE otherwise.  */
14986169689Skan
14987132718Skanstatic tree
14988169689Skancp_parser_lookup_name (cp_parser *parser, tree name,
14989169689Skan		       enum tag_types tag_type,
14990169689Skan		       bool is_template,
14991169689Skan		       bool is_namespace,
14992169689Skan		       bool check_dependency,
14993169689Skan		       tree *ambiguous_decls)
14994132718Skan{
14995161651Skan  int flags = 0;
14996132718Skan  tree decl;
14997132718Skan  tree object_type = parser->context->object_type;
14998132718Skan
14999169689Skan  if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
15000169689Skan    flags |= LOOKUP_COMPLAIN;
15001169689Skan
15002169689Skan  /* Assume that the lookup will be unambiguous.  */
15003169689Skan  if (ambiguous_decls)
15004169689Skan    *ambiguous_decls = NULL_TREE;
15005169689Skan
15006132718Skan  /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
15007132718Skan     no longer valid.  Note that if we are parsing tentatively, and
15008132718Skan     the parse fails, OBJECT_TYPE will be automatically restored.  */
15009132718Skan  parser->context->object_type = NULL_TREE;
15010132718Skan
15011132718Skan  if (name == error_mark_node)
15012132718Skan    return error_mark_node;
15013132718Skan
15014132718Skan  /* A template-id has already been resolved; there is no lookup to
15015132718Skan     do.  */
15016132718Skan  if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
15017132718Skan    return name;
15018132718Skan  if (BASELINK_P (name))
15019132718Skan    {
15020169689Skan      gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
15021169689Skan		  == TEMPLATE_ID_EXPR);
15022132718Skan      return name;
15023132718Skan    }
15024132718Skan
15025132718Skan  /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
15026132718Skan     it should already have been checked to make sure that the name
15027132718Skan     used matches the type being destroyed.  */
15028132718Skan  if (TREE_CODE (name) == BIT_NOT_EXPR)
15029132718Skan    {
15030132718Skan      tree type;
15031132718Skan
15032132718Skan      /* Figure out to which type this destructor applies.  */
15033132718Skan      if (parser->scope)
15034132718Skan	type = parser->scope;
15035132718Skan      else if (object_type)
15036132718Skan	type = object_type;
15037132718Skan      else
15038132718Skan	type = current_class_type;
15039132718Skan      /* If that's not a class type, there is no destructor.  */
15040132718Skan      if (!type || !CLASS_TYPE_P (type))
15041132718Skan	return error_mark_node;
15042169689Skan      if (CLASSTYPE_LAZY_DESTRUCTOR (type))
15043169689Skan	lazily_declare_fn (sfk_destructor, type);
15044132718Skan      if (!CLASSTYPE_DESTRUCTORS (type))
15045132718Skan	  return error_mark_node;
15046132718Skan      /* If it was a class type, return the destructor.  */
15047132718Skan      return CLASSTYPE_DESTRUCTORS (type);
15048132718Skan    }
15049132718Skan
15050132718Skan  /* By this point, the NAME should be an ordinary identifier.  If
15051132718Skan     the id-expression was a qualified name, the qualifying scope is
15052132718Skan     stored in PARSER->SCOPE at this point.  */
15053169689Skan  gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
15054169689Skan
15055132718Skan  /* Perform the lookup.  */
15056132718Skan  if (parser->scope)
15057169689Skan    {
15058132718Skan      bool dependent_p;
15059132718Skan
15060132718Skan      if (parser->scope == error_mark_node)
15061132718Skan	return error_mark_node;
15062132718Skan
15063132718Skan      /* If the SCOPE is dependent, the lookup must be deferred until
15064132718Skan	 the template is instantiated -- unless we are explicitly
15065132718Skan	 looking up names in uninstantiated templates.  Even then, we
15066132718Skan	 cannot look up the name if the scope is not a class type; it
15067132718Skan	 might, for example, be a template type parameter.  */
15068132718Skan      dependent_p = (TYPE_P (parser->scope)
15069132718Skan		     && !(parser->in_declarator_p
15070132718Skan			  && currently_open_class (parser->scope))
15071132718Skan		     && dependent_type_p (parser->scope));
15072132718Skan      if ((check_dependency || !CLASS_TYPE_P (parser->scope))
15073132718Skan	   && dependent_p)
15074132718Skan	{
15075169689Skan	  if (tag_type)
15076169689Skan	    {
15077169689Skan	      tree type;
15078169689Skan
15079169689Skan	      /* The resolution to Core Issue 180 says that `struct
15080169689Skan		 A::B' should be considered a type-name, even if `A'
15081169689Skan		 is dependent.  */
15082169689Skan	      type = make_typename_type (parser->scope, name, tag_type,
15083169689Skan					 /*complain=*/tf_error);
15084169689Skan	      decl = TYPE_NAME (type);
15085169689Skan	    }
15086169689Skan	  else if (is_template
15087169689Skan		   && (cp_parser_next_token_ends_template_argument_p (parser)
15088169689Skan		       || cp_lexer_next_token_is (parser->lexer,
15089169689Skan						  CPP_CLOSE_PAREN)))
15090132718Skan	    decl = make_unbound_class_template (parser->scope,
15091169689Skan						name, NULL_TREE,
15092169689Skan						/*complain=*/tf_error);
15093132718Skan	  else
15094169689Skan	    decl = build_qualified_name (/*type=*/NULL_TREE,
15095169689Skan					 parser->scope, name,
15096169689Skan					 is_template);
15097132718Skan	}
15098132718Skan      else
15099132718Skan	{
15100169689Skan	  tree pushed_scope = NULL_TREE;
15101132718Skan
15102132718Skan	  /* If PARSER->SCOPE is a dependent type, then it must be a
15103132718Skan	     class type, and we must not be checking dependencies;
15104132718Skan	     otherwise, we would have processed this lookup above.  So
15105132718Skan	     that PARSER->SCOPE is not considered a dependent base by
15106132718Skan	     lookup_member, we must enter the scope here.  */
15107132718Skan	  if (dependent_p)
15108169689Skan	    pushed_scope = push_scope (parser->scope);
15109169689Skan	  /* If the PARSER->SCOPE is a template specialization, it
15110132718Skan	     may be instantiated during name lookup.  In that case,
15111132718Skan	     errors may be issued.  Even if we rollback the current
15112132718Skan	     tentative parse, those errors are valid.  */
15113169689Skan	  decl = lookup_qualified_name (parser->scope, name,
15114169689Skan					tag_type != none_type,
15115132718Skan					/*complain=*/true);
15116169689Skan	  if (pushed_scope)
15117169689Skan	    pop_scope (pushed_scope);
15118132718Skan	}
15119132718Skan      parser->qualifying_scope = parser->scope;
15120132718Skan      parser->object_scope = NULL_TREE;
15121132718Skan    }
15122132718Skan  else if (object_type)
15123132718Skan    {
15124132718Skan      tree object_decl = NULL_TREE;
15125132718Skan      /* Look up the name in the scope of the OBJECT_TYPE, unless the
15126132718Skan	 OBJECT_TYPE is not a class.  */
15127132718Skan      if (CLASS_TYPE_P (object_type))
15128132718Skan	/* If the OBJECT_TYPE is a template specialization, it may
15129132718Skan	   be instantiated during name lookup.  In that case, errors
15130132718Skan	   may be issued.  Even if we rollback the current tentative
15131132718Skan	   parse, those errors are valid.  */
15132132718Skan	object_decl = lookup_member (object_type,
15133132718Skan				     name,
15134169689Skan				     /*protect=*/0,
15135169689Skan				     tag_type != none_type);
15136132718Skan      /* Look it up in the enclosing context, too.  */
15137169689Skan      decl = lookup_name_real (name, tag_type != none_type,
15138169689Skan			       /*nonclass=*/0,
15139169689Skan			       /*block_p=*/true, is_namespace, flags);
15140132718Skan      parser->object_scope = object_type;
15141132718Skan      parser->qualifying_scope = NULL_TREE;
15142132718Skan      if (object_decl)
15143132718Skan	decl = object_decl;
15144132718Skan    }
15145132718Skan  else
15146132718Skan    {
15147169689Skan      decl = lookup_name_real (name, tag_type != none_type,
15148169689Skan			       /*nonclass=*/0,
15149169689Skan			       /*block_p=*/true, is_namespace, flags);
15150132718Skan      parser->qualifying_scope = NULL_TREE;
15151132718Skan      parser->object_scope = NULL_TREE;
15152132718Skan    }
15153132718Skan
15154132718Skan  /* If the lookup failed, let our caller know.  */
15155169689Skan  if (!decl || decl == error_mark_node)
15156132718Skan    return error_mark_node;
15157132718Skan
15158132718Skan  /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
15159132718Skan  if (TREE_CODE (decl) == TREE_LIST)
15160132718Skan    {
15161169689Skan      if (ambiguous_decls)
15162169689Skan	*ambiguous_decls = decl;
15163132718Skan      /* The error message we have to print is too complicated for
15164132718Skan	 cp_parser_error, so we incorporate its actions directly.  */
15165132718Skan      if (!cp_parser_simulate_error (parser))
15166132718Skan	{
15167169689Skan	  error ("reference to %qD is ambiguous", name);
15168132718Skan	  print_candidates (decl);
15169132718Skan	}
15170132718Skan      return error_mark_node;
15171132718Skan    }
15172132718Skan
15173169689Skan  gcc_assert (DECL_P (decl)
15174169689Skan	      || TREE_CODE (decl) == OVERLOAD
15175169689Skan	      || TREE_CODE (decl) == SCOPE_REF
15176169689Skan	      || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
15177169689Skan	      || BASELINK_P (decl));
15178132718Skan
15179132718Skan  /* If we have resolved the name of a member declaration, check to
15180132718Skan     see if the declaration is accessible.  When the name resolves to
15181132718Skan     set of overloaded functions, accessibility is checked when
15182169689Skan     overload resolution is done.
15183132718Skan
15184132718Skan     During an explicit instantiation, access is not checked at all,
15185132718Skan     as per [temp.explicit].  */
15186132718Skan  if (DECL_P (decl))
15187132718Skan    check_accessibility_of_qualified_id (decl, object_type, parser->scope);
15188132718Skan
15189132718Skan  return decl;
15190132718Skan}
15191132718Skan
15192132718Skan/* Like cp_parser_lookup_name, but for use in the typical case where
15193132718Skan   CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
15194132718Skan   IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
15195132718Skan
15196132718Skanstatic tree
15197132718Skancp_parser_lookup_name_simple (cp_parser* parser, tree name)
15198132718Skan{
15199169689Skan  return cp_parser_lookup_name (parser, name,
15200169689Skan				none_type,
15201132718Skan				/*is_template=*/false,
15202132718Skan				/*is_namespace=*/false,
15203169689Skan				/*check_dependency=*/true,
15204169689Skan				/*ambiguous_decls=*/NULL);
15205132718Skan}
15206132718Skan
15207132718Skan/* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
15208132718Skan   the current context, return the TYPE_DECL.  If TAG_NAME_P is
15209132718Skan   true, the DECL indicates the class being defined in a class-head,
15210132718Skan   or declared in an elaborated-type-specifier.
15211132718Skan
15212132718Skan   Otherwise, return DECL.  */
15213132718Skan
15214132718Skanstatic tree
15215132718Skancp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
15216132718Skan{
15217132718Skan  /* If the TEMPLATE_DECL is being declared as part of a class-head,
15218132718Skan     the translation from TEMPLATE_DECL to TYPE_DECL occurs:
15219132718Skan
15220169689Skan       struct A {
15221169689Skan	 template <typename T> struct B;
15222132718Skan       };
15223132718Skan
15224169689Skan       template <typename T> struct A::B {};
15225132718Skan
15226169689Skan     Similarly, in an elaborated-type-specifier:
15227169689Skan
15228132718Skan       namespace N { struct X{}; }
15229132718Skan
15230132718Skan       struct A {
15231169689Skan	 template <typename T> friend struct N::X;
15232132718Skan       };
15233132718Skan
15234132718Skan     However, if the DECL refers to a class type, and we are in
15235132718Skan     the scope of the class, then the name lookup automatically
15236132718Skan     finds the TYPE_DECL created by build_self_reference rather
15237132718Skan     than a TEMPLATE_DECL.  For example, in:
15238132718Skan
15239132718Skan       template <class T> struct S {
15240169689Skan	 S s;
15241132718Skan       };
15242132718Skan
15243132718Skan     there is no need to handle such case.  */
15244132718Skan
15245132718Skan  if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
15246132718Skan    return DECL_TEMPLATE_RESULT (decl);
15247132718Skan
15248132718Skan  return decl;
15249132718Skan}
15250132718Skan
15251132718Skan/* If too many, or too few, template-parameter lists apply to the
15252132718Skan   declarator, issue an error message.  Returns TRUE if all went well,
15253132718Skan   and FALSE otherwise.  */
15254132718Skan
15255132718Skanstatic bool
15256169689Skancp_parser_check_declarator_template_parameters (cp_parser* parser,
15257169689Skan						cp_declarator *declarator)
15258132718Skan{
15259132718Skan  unsigned num_templates;
15260132718Skan
15261132718Skan  /* We haven't seen any classes that involve template parameters yet.  */
15262132718Skan  num_templates = 0;
15263132718Skan
15264169689Skan  switch (declarator->kind)
15265132718Skan    {
15266169689Skan    case cdk_id:
15267169689Skan      if (declarator->u.id.qualifying_scope)
15268169689Skan	{
15269169689Skan	  tree scope;
15270169689Skan	  tree member;
15271132718Skan
15272169689Skan	  scope = declarator->u.id.qualifying_scope;
15273169689Skan	  member = declarator->u.id.unqualified_name;
15274132718Skan
15275169689Skan	  while (scope && CLASS_TYPE_P (scope))
15276169689Skan	    {
15277169689Skan	      /* You're supposed to have one `template <...>'
15278169689Skan		 for every template class, but you don't need one
15279169689Skan		 for a full specialization.  For example:
15280132718Skan
15281169689Skan		 template <class T> struct S{};
15282169689Skan		 template <> struct S<int> { void f(); };
15283169689Skan		 void S<int>::f () {}
15284132718Skan
15285169689Skan		 is correct; there shouldn't be a `template <>' for
15286169689Skan		 the definition of `S<int>::f'.  */
15287169689Skan	      if (!CLASSTYPE_TEMPLATE_INFO (scope))
15288169689Skan		/* If SCOPE does not have template information of any
15289169689Skan		   kind, then it is not a template, nor is it nested
15290169689Skan		   within a template.  */
15291169689Skan		break;
15292169689Skan	      if (explicit_class_specialization_p (scope))
15293169689Skan		break;
15294169689Skan	      if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
15295169689Skan		++num_templates;
15296132718Skan
15297169689Skan	      scope = TYPE_CONTEXT (scope);
15298169689Skan	    }
15299169689Skan	}
15300169689Skan      else if (TREE_CODE (declarator->u.id.unqualified_name)
15301169689Skan	       == TEMPLATE_ID_EXPR)
15302169689Skan	/* If the DECLARATOR has the form `X<y>' then it uses one
15303169689Skan	   additional level of template parameters.  */
15304169689Skan	++num_templates;
15305132718Skan
15306169689Skan      return cp_parser_check_template_parameters (parser,
15307169689Skan						  num_templates);
15308132718Skan
15309169689Skan    case cdk_function:
15310169689Skan    case cdk_array:
15311169689Skan    case cdk_pointer:
15312169689Skan    case cdk_reference:
15313169689Skan    case cdk_ptrmem:
15314169689Skan      return (cp_parser_check_declarator_template_parameters
15315169689Skan	      (parser, declarator->declarator));
15316169689Skan
15317169689Skan    case cdk_error:
15318169689Skan      return true;
15319169689Skan
15320132718Skan    default:
15321169689Skan      gcc_unreachable ();
15322132718Skan    }
15323169689Skan  return false;
15324132718Skan}
15325132718Skan
15326132718Skan/* NUM_TEMPLATES were used in the current declaration.  If that is
15327132718Skan   invalid, return FALSE and issue an error messages.  Otherwise,
15328132718Skan   return TRUE.  */
15329132718Skan
15330132718Skanstatic bool
15331132718Skancp_parser_check_template_parameters (cp_parser* parser,
15332169689Skan				     unsigned num_templates)
15333132718Skan{
15334132718Skan  /* If there are more template classes than parameter lists, we have
15335132718Skan     something like:
15336169689Skan
15337132718Skan       template <class T> void S<T>::R<T>::f ();  */
15338132718Skan  if (parser->num_template_parameter_lists < num_templates)
15339132718Skan    {
15340132718Skan      error ("too few template-parameter-lists");
15341132718Skan      return false;
15342132718Skan    }
15343132718Skan  /* If there are the same number of template classes and parameter
15344132718Skan     lists, that's OK.  */
15345132718Skan  if (parser->num_template_parameter_lists == num_templates)
15346132718Skan    return true;
15347132718Skan  /* If there are more, but only one more, then we are referring to a
15348132718Skan     member template.  That's OK too.  */
15349132718Skan  if (parser->num_template_parameter_lists == num_templates + 1)
15350132718Skan      return true;
15351132718Skan  /* Otherwise, there are too many template parameter lists.  We have
15352132718Skan     something like:
15353132718Skan
15354132718Skan     template <class T> template <class U> void S::f();  */
15355132718Skan  error ("too many template-parameter-lists");
15356132718Skan  return false;
15357132718Skan}
15358132718Skan
15359132718Skan/* Parse an optional `::' token indicating that the following name is
15360132718Skan   from the global namespace.  If so, PARSER->SCOPE is set to the
15361132718Skan   GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
15362132718Skan   unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
15363132718Skan   Returns the new value of PARSER->SCOPE, if the `::' token is
15364132718Skan   present, and NULL_TREE otherwise.  */
15365132718Skan
15366132718Skanstatic tree
15367132718Skancp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
15368132718Skan{
15369132718Skan  cp_token *token;
15370132718Skan
15371132718Skan  /* Peek at the next token.  */
15372132718Skan  token = cp_lexer_peek_token (parser->lexer);
15373132718Skan  /* If we're looking at a `::' token then we're starting from the
15374132718Skan     global namespace, not our current location.  */
15375132718Skan  if (token->type == CPP_SCOPE)
15376132718Skan    {
15377132718Skan      /* Consume the `::' token.  */
15378132718Skan      cp_lexer_consume_token (parser->lexer);
15379132718Skan      /* Set the SCOPE so that we know where to start the lookup.  */
15380132718Skan      parser->scope = global_namespace;
15381132718Skan      parser->qualifying_scope = global_namespace;
15382132718Skan      parser->object_scope = NULL_TREE;
15383132718Skan
15384132718Skan      return parser->scope;
15385132718Skan    }
15386132718Skan  else if (!current_scope_valid_p)
15387132718Skan    {
15388132718Skan      parser->scope = NULL_TREE;
15389132718Skan      parser->qualifying_scope = NULL_TREE;
15390132718Skan      parser->object_scope = NULL_TREE;
15391132718Skan    }
15392132718Skan
15393132718Skan  return NULL_TREE;
15394132718Skan}
15395132718Skan
15396132718Skan/* Returns TRUE if the upcoming token sequence is the start of a
15397132718Skan   constructor declarator.  If FRIEND_P is true, the declarator is
15398132718Skan   preceded by the `friend' specifier.  */
15399132718Skan
15400132718Skanstatic bool
15401132718Skancp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
15402132718Skan{
15403132718Skan  bool constructor_p;
15404132718Skan  tree type_decl = NULL_TREE;
15405132718Skan  bool nested_name_p;
15406132718Skan  cp_token *next_token;
15407132718Skan
15408132718Skan  /* The common case is that this is not a constructor declarator, so
15409132718Skan     try to avoid doing lots of work if at all possible.  It's not
15410132718Skan     valid declare a constructor at function scope.  */
15411169689Skan  if (parser->in_function_body)
15412132718Skan    return false;
15413132718Skan  /* And only certain tokens can begin a constructor declarator.  */
15414132718Skan  next_token = cp_lexer_peek_token (parser->lexer);
15415132718Skan  if (next_token->type != CPP_NAME
15416132718Skan      && next_token->type != CPP_SCOPE
15417132718Skan      && next_token->type != CPP_NESTED_NAME_SPECIFIER
15418132718Skan      && next_token->type != CPP_TEMPLATE_ID)
15419132718Skan    return false;
15420132718Skan
15421132718Skan  /* Parse tentatively; we are going to roll back all of the tokens
15422132718Skan     consumed here.  */
15423132718Skan  cp_parser_parse_tentatively (parser);
15424132718Skan  /* Assume that we are looking at a constructor declarator.  */
15425132718Skan  constructor_p = true;
15426132718Skan
15427132718Skan  /* Look for the optional `::' operator.  */
15428132718Skan  cp_parser_global_scope_opt (parser,
15429132718Skan			      /*current_scope_valid_p=*/false);
15430132718Skan  /* Look for the nested-name-specifier.  */
15431169689Skan  nested_name_p
15432132718Skan    = (cp_parser_nested_name_specifier_opt (parser,
15433132718Skan					    /*typename_keyword_p=*/false,
15434132718Skan					    /*check_dependency_p=*/false,
15435132718Skan					    /*type_p=*/false,
15436132718Skan					    /*is_declaration=*/false)
15437132718Skan       != NULL_TREE);
15438132718Skan  /* Outside of a class-specifier, there must be a
15439132718Skan     nested-name-specifier.  */
15440169689Skan  if (!nested_name_p &&
15441132718Skan      (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
15442132718Skan       || friend_p))
15443132718Skan    constructor_p = false;
15444132718Skan  /* If we still think that this might be a constructor-declarator,
15445132718Skan     look for a class-name.  */
15446132718Skan  if (constructor_p)
15447132718Skan    {
15448132718Skan      /* If we have:
15449132718Skan
15450132718Skan	   template <typename T> struct S { S(); };
15451132718Skan	   template <typename T> S<T>::S ();
15452132718Skan
15453132718Skan	 we must recognize that the nested `S' names a class.
15454132718Skan	 Similarly, for:
15455132718Skan
15456132718Skan	   template <typename T> S<T>::S<T> ();
15457132718Skan
15458132718Skan	 we must recognize that the nested `S' names a template.  */
15459132718Skan      type_decl = cp_parser_class_name (parser,
15460132718Skan					/*typename_keyword_p=*/false,
15461132718Skan					/*template_keyword_p=*/false,
15462169689Skan					none_type,
15463132718Skan					/*check_dependency_p=*/false,
15464132718Skan					/*class_head_p=*/false,
15465132718Skan					/*is_declaration=*/false);
15466132718Skan      /* If there was no class-name, then this is not a constructor.  */
15467132718Skan      constructor_p = !cp_parser_error_occurred (parser);
15468132718Skan    }
15469132718Skan
15470132718Skan  /* If we're still considering a constructor, we have to see a `(',
15471132718Skan     to begin the parameter-declaration-clause, followed by either a
15472132718Skan     `)', an `...', or a decl-specifier.  We need to check for a
15473132718Skan     type-specifier to avoid being fooled into thinking that:
15474132718Skan
15475132718Skan       S::S (f) (int);
15476132718Skan
15477132718Skan     is a constructor.  (It is actually a function named `f' that
15478132718Skan     takes one parameter (of type `int') and returns a value of type
15479132718Skan     `S::S'.  */
15480169689Skan  if (constructor_p
15481132718Skan      && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
15482132718Skan    {
15483132718Skan      if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
15484132718Skan	  && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
15485132718Skan	  /* A parameter declaration begins with a decl-specifier,
15486132718Skan	     which is either the "attribute" keyword, a storage class
15487132718Skan	     specifier, or (usually) a type-specifier.  */
15488169689Skan	  && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
15489132718Skan	{
15490132718Skan	  tree type;
15491169689Skan	  tree pushed_scope = NULL_TREE;
15492132718Skan	  unsigned saved_num_template_parameter_lists;
15493132718Skan
15494132718Skan	  /* Names appearing in the type-specifier should be looked up
15495132718Skan	     in the scope of the class.  */
15496132718Skan	  if (current_class_type)
15497132718Skan	    type = NULL_TREE;
15498132718Skan	  else
15499132718Skan	    {
15500132718Skan	      type = TREE_TYPE (type_decl);
15501132718Skan	      if (TREE_CODE (type) == TYPENAME_TYPE)
15502132718Skan		{
15503169689Skan		  type = resolve_typename_type (type,
15504132718Skan						/*only_current_p=*/false);
15505132718Skan		  if (type == error_mark_node)
15506132718Skan		    {
15507132718Skan		      cp_parser_abort_tentative_parse (parser);
15508132718Skan		      return false;
15509132718Skan		    }
15510132718Skan		}
15511169689Skan	      pushed_scope = push_scope (type);
15512132718Skan	    }
15513132718Skan
15514132718Skan	  /* Inside the constructor parameter list, surrounding
15515132718Skan	     template-parameter-lists do not apply.  */
15516132718Skan	  saved_num_template_parameter_lists
15517132718Skan	    = parser->num_template_parameter_lists;
15518132718Skan	  parser->num_template_parameter_lists = 0;
15519132718Skan
15520132718Skan	  /* Look for the type-specifier.  */
15521132718Skan	  cp_parser_type_specifier (parser,
15522132718Skan				    CP_PARSER_FLAGS_NONE,
15523169689Skan				    /*decl_specs=*/NULL,
15524132718Skan				    /*is_declarator=*/true,
15525132718Skan				    /*declares_class_or_enum=*/NULL,
15526132718Skan				    /*is_cv_qualifier=*/NULL);
15527132718Skan
15528132718Skan	  parser->num_template_parameter_lists
15529132718Skan	    = saved_num_template_parameter_lists;
15530132718Skan
15531132718Skan	  /* Leave the scope of the class.  */
15532169689Skan	  if (pushed_scope)
15533169689Skan	    pop_scope (pushed_scope);
15534132718Skan
15535132718Skan	  constructor_p = !cp_parser_error_occurred (parser);
15536132718Skan	}
15537132718Skan    }
15538132718Skan  else
15539132718Skan    constructor_p = false;
15540132718Skan  /* We did not really want to consume any tokens.  */
15541132718Skan  cp_parser_abort_tentative_parse (parser);
15542132718Skan
15543132718Skan  return constructor_p;
15544132718Skan}
15545132718Skan
15546132718Skan/* Parse the definition of the function given by the DECL_SPECIFIERS,
15547132718Skan   ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
15548132718Skan   they must be performed once we are in the scope of the function.
15549132718Skan
15550132718Skan   Returns the function defined.  */
15551132718Skan
15552132718Skanstatic tree
15553132718Skancp_parser_function_definition_from_specifiers_and_declarator
15554132718Skan  (cp_parser* parser,
15555169689Skan   cp_decl_specifier_seq *decl_specifiers,
15556132718Skan   tree attributes,
15557169689Skan   const cp_declarator *declarator)
15558132718Skan{
15559132718Skan  tree fn;
15560132718Skan  bool success_p;
15561132718Skan
15562132718Skan  /* Begin the function-definition.  */
15563169689Skan  success_p = start_function (decl_specifiers, declarator, attributes);
15564132718Skan
15565169689Skan  /* The things we're about to see are not directly qualified by any
15566169689Skan     template headers we've seen thus far.  */
15567169689Skan  reset_specialization ();
15568169689Skan
15569132718Skan  /* If there were names looked up in the decl-specifier-seq that we
15570132718Skan     did not check, check them now.  We must wait until we are in the
15571132718Skan     scope of the function to perform the checks, since the function
15572132718Skan     might be a friend.  */
15573132718Skan  perform_deferred_access_checks ();
15574132718Skan
15575132718Skan  if (!success_p)
15576132718Skan    {
15577169689Skan      /* Skip the entire function.  */
15578132718Skan      cp_parser_skip_to_end_of_block_or_statement (parser);
15579132718Skan      fn = error_mark_node;
15580132718Skan    }
15581132718Skan  else
15582132718Skan    fn = cp_parser_function_definition_after_declarator (parser,
15583132718Skan							 /*inline_p=*/false);
15584132718Skan
15585132718Skan  return fn;
15586132718Skan}
15587132718Skan
15588132718Skan/* Parse the part of a function-definition that follows the
15589132718Skan   declarator.  INLINE_P is TRUE iff this function is an inline
15590132718Skan   function defined with a class-specifier.
15591132718Skan
15592132718Skan   Returns the function defined.  */
15593132718Skan
15594169689Skanstatic tree
15595169689Skancp_parser_function_definition_after_declarator (cp_parser* parser,
15596132718Skan						bool inline_p)
15597132718Skan{
15598132718Skan  tree fn;
15599132718Skan  bool ctor_initializer_p = false;
15600132718Skan  bool saved_in_unbraced_linkage_specification_p;
15601169689Skan  bool saved_in_function_body;
15602132718Skan  unsigned saved_num_template_parameter_lists;
15603132718Skan
15604169689Skan  saved_in_function_body = parser->in_function_body;
15605169689Skan  parser->in_function_body = true;
15606132718Skan  /* If the next token is `return', then the code may be trying to
15607132718Skan     make use of the "named return value" extension that G++ used to
15608132718Skan     support.  */
15609132718Skan  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
15610132718Skan    {
15611132718Skan      /* Consume the `return' keyword.  */
15612132718Skan      cp_lexer_consume_token (parser->lexer);
15613132718Skan      /* Look for the identifier that indicates what value is to be
15614132718Skan	 returned.  */
15615132718Skan      cp_parser_identifier (parser);
15616132718Skan      /* Issue an error message.  */
15617132718Skan      error ("named return values are no longer supported");
15618132718Skan      /* Skip tokens until we reach the start of the function body.  */
15619169689Skan      while (true)
15620169689Skan	{
15621169689Skan	  cp_token *token = cp_lexer_peek_token (parser->lexer);
15622169689Skan	  if (token->type == CPP_OPEN_BRACE
15623169689Skan	      || token->type == CPP_EOF
15624169689Skan	      || token->type == CPP_PRAGMA_EOL)
15625169689Skan	    break;
15626169689Skan	  cp_lexer_consume_token (parser->lexer);
15627169689Skan	}
15628132718Skan    }
15629132718Skan  /* The `extern' in `extern "C" void f () { ... }' does not apply to
15630132718Skan     anything declared inside `f'.  */
15631169689Skan  saved_in_unbraced_linkage_specification_p
15632132718Skan    = parser->in_unbraced_linkage_specification_p;
15633132718Skan  parser->in_unbraced_linkage_specification_p = false;
15634132718Skan  /* Inside the function, surrounding template-parameter-lists do not
15635132718Skan     apply.  */
15636169689Skan  saved_num_template_parameter_lists
15637169689Skan    = parser->num_template_parameter_lists;
15638132718Skan  parser->num_template_parameter_lists = 0;
15639132718Skan  /* If the next token is `try', then we are looking at a
15640132718Skan     function-try-block.  */
15641132718Skan  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
15642132718Skan    ctor_initializer_p = cp_parser_function_try_block (parser);
15643132718Skan  /* A function-try-block includes the function-body, so we only do
15644132718Skan     this next part if we're not processing a function-try-block.  */
15645132718Skan  else
15646169689Skan    ctor_initializer_p
15647132718Skan      = cp_parser_ctor_initializer_opt_and_function_body (parser);
15648132718Skan
15649132718Skan  /* Finish the function.  */
15650169689Skan  fn = finish_function ((ctor_initializer_p ? 1 : 0) |
15651132718Skan			(inline_p ? 2 : 0));
15652132718Skan  /* Generate code for it, if necessary.  */
15653132718Skan  expand_or_defer_fn (fn);
15654132718Skan  /* Restore the saved values.  */
15655169689Skan  parser->in_unbraced_linkage_specification_p
15656132718Skan    = saved_in_unbraced_linkage_specification_p;
15657169689Skan  parser->num_template_parameter_lists
15658132718Skan    = saved_num_template_parameter_lists;
15659169689Skan  parser->in_function_body = saved_in_function_body;
15660132718Skan
15661132718Skan  return fn;
15662132718Skan}
15663132718Skan
15664132718Skan/* Parse a template-declaration, assuming that the `export' (and
15665132718Skan   `extern') keywords, if present, has already been scanned.  MEMBER_P
15666132718Skan   is as for cp_parser_template_declaration.  */
15667132718Skan
15668132718Skanstatic void
15669132718Skancp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
15670132718Skan{
15671132718Skan  tree decl = NULL_TREE;
15672169689Skan  VEC (deferred_access_check,gc) *checks;
15673132718Skan  tree parameter_list;
15674132718Skan  bool friend_p = false;
15675169689Skan  bool need_lang_pop;
15676132718Skan
15677132718Skan  /* Look for the `template' keyword.  */
15678132718Skan  if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
15679132718Skan    return;
15680169689Skan
15681132718Skan  /* And the `<'.  */
15682132718Skan  if (!cp_parser_require (parser, CPP_LESS, "`<'"))
15683132718Skan    return;
15684169689Skan  if (at_class_scope_p () && current_function_decl)
15685169689Skan    {
15686169689Skan      /* 14.5.2.2 [temp.mem]
15687169689Skan
15688169689Skan         A local class shall not have member templates.  */
15689169689Skan      error ("invalid declaration of member template in local class");
15690169689Skan      cp_parser_skip_to_end_of_block_or_statement (parser);
15691169689Skan      return;
15692169689Skan    }
15693169689Skan  /* [temp]
15694169689Skan
15695169689Skan     A template ... shall not have C linkage.  */
15696169689Skan  if (current_lang_name == lang_name_c)
15697169689Skan    {
15698169689Skan      error ("template with C linkage");
15699169689Skan      /* Give it C++ linkage to avoid confusing other parts of the
15700169689Skan	 front end.  */
15701169689Skan      push_lang_context (lang_name_cplusplus);
15702169689Skan      need_lang_pop = true;
15703169689Skan    }
15704169689Skan  else
15705169689Skan    need_lang_pop = false;
15706169689Skan
15707169689Skan  /* We cannot perform access checks on the template parameter
15708169689Skan     declarations until we know what is being declared, just as we
15709169689Skan     cannot check the decl-specifier list.  */
15710169689Skan  push_deferring_access_checks (dk_deferred);
15711169689Skan
15712132718Skan  /* If the next token is `>', then we have an invalid
15713132718Skan     specialization.  Rather than complain about an invalid template
15714132718Skan     parameter, issue an error message here.  */
15715132718Skan  if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15716132718Skan    {
15717132718Skan      cp_parser_error (parser, "invalid explicit specialization");
15718132718Skan      begin_specialization ();
15719132718Skan      parameter_list = NULL_TREE;
15720132718Skan    }
15721132718Skan  else
15722169689Skan    /* Parse the template parameters.  */
15723169689Skan    parameter_list = cp_parser_template_parameter_list (parser);
15724132718Skan
15725169689Skan  /* Get the deferred access checks from the parameter list.  These
15726169689Skan     will be checked once we know what is being declared, as for a
15727169689Skan     member template the checks must be performed in the scope of the
15728169689Skan     class containing the member.  */
15729169689Skan  checks = get_deferred_access_checks ();
15730169689Skan
15731132718Skan  /* Look for the `>'.  */
15732169689Skan  cp_parser_skip_to_end_of_template_parameter_list (parser);
15733132718Skan  /* We just processed one more parameter list.  */
15734132718Skan  ++parser->num_template_parameter_lists;
15735132718Skan  /* If the next token is `template', there are more template
15736132718Skan     parameters.  */
15737169689Skan  if (cp_lexer_next_token_is_keyword (parser->lexer,
15738132718Skan				      RID_TEMPLATE))
15739132718Skan    cp_parser_template_declaration_after_export (parser, member_p);
15740132718Skan  else
15741132718Skan    {
15742169689Skan      /* There are no access checks when parsing a template, as we do not
15743169689Skan	 know if a specialization will be a friend.  */
15744169689Skan      push_deferring_access_checks (dk_no_check);
15745132718Skan      decl = cp_parser_single_declaration (parser,
15746169689Skan					   checks,
15747132718Skan					   member_p,
15748132718Skan					   &friend_p);
15749169689Skan      pop_deferring_access_checks ();
15750132718Skan
15751132718Skan      /* If this is a member template declaration, let the front
15752132718Skan	 end know.  */
15753132718Skan      if (member_p && !friend_p && decl)
15754132718Skan	{
15755132718Skan	  if (TREE_CODE (decl) == TYPE_DECL)
15756132718Skan	    cp_parser_check_access_in_redeclaration (decl);
15757132718Skan
15758132718Skan	  decl = finish_member_template_decl (decl);
15759132718Skan	}
15760132718Skan      else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
15761132718Skan	make_friend_class (current_class_type, TREE_TYPE (decl),
15762132718Skan			   /*complain=*/true);
15763132718Skan    }
15764132718Skan  /* We are done with the current parameter list.  */
15765132718Skan  --parser->num_template_parameter_lists;
15766132718Skan
15767169689Skan  pop_deferring_access_checks ();
15768169689Skan
15769132718Skan  /* Finish up.  */
15770132718Skan  finish_template_decl (parameter_list);
15771132718Skan
15772132718Skan  /* Register member declarations.  */
15773132718Skan  if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
15774132718Skan    finish_member_declaration (decl);
15775169689Skan  /* For the erroneous case of a template with C linkage, we pushed an
15776169689Skan     implicit C++ linkage scope; exit that scope now.  */
15777169689Skan  if (need_lang_pop)
15778169689Skan    pop_lang_context ();
15779132718Skan  /* If DECL is a function template, we must return to parse it later.
15780132718Skan     (Even though there is no definition, there might be default
15781132718Skan     arguments that need handling.)  */
15782169689Skan  if (member_p && decl
15783132718Skan      && (TREE_CODE (decl) == FUNCTION_DECL
15784132718Skan	  || DECL_FUNCTION_TEMPLATE_P (decl)))
15785132718Skan    TREE_VALUE (parser->unparsed_functions_queues)
15786169689Skan      = tree_cons (NULL_TREE, decl,
15787132718Skan		   TREE_VALUE (parser->unparsed_functions_queues));
15788132718Skan}
15789132718Skan
15790169689Skan/* Perform the deferred access checks from a template-parameter-list.
15791169689Skan   CHECKS is a TREE_LIST of access checks, as returned by
15792169689Skan   get_deferred_access_checks.  */
15793169689Skan
15794169689Skanstatic void
15795169689Skancp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
15796169689Skan{
15797169689Skan  ++processing_template_parmlist;
15798169689Skan  perform_access_checks (checks);
15799169689Skan  --processing_template_parmlist;
15800169689Skan}
15801169689Skan
15802132718Skan/* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
15803132718Skan   `function-definition' sequence.  MEMBER_P is true, this declaration
15804132718Skan   appears in a class scope.
15805132718Skan
15806132718Skan   Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
15807132718Skan   *FRIEND_P is set to TRUE iff the declaration is a friend.  */
15808132718Skan
15809132718Skanstatic tree
15810169689Skancp_parser_single_declaration (cp_parser* parser,
15811169689Skan			      VEC (deferred_access_check,gc)* checks,
15812132718Skan			      bool member_p,
15813132718Skan			      bool* friend_p)
15814132718Skan{
15815132718Skan  int declares_class_or_enum;
15816132718Skan  tree decl = NULL_TREE;
15817169689Skan  cp_decl_specifier_seq decl_specifiers;
15818132718Skan  bool function_definition_p = false;
15819132718Skan
15820161651Skan  /* This function is only used when processing a template
15821161651Skan     declaration.  */
15822169689Skan  gcc_assert (innermost_scope_kind () == sk_template_parms
15823169689Skan	      || innermost_scope_kind () == sk_template_spec);
15824161651Skan
15825132718Skan  /* Defer access checks until we know what is being declared.  */
15826132718Skan  push_deferring_access_checks (dk_deferred);
15827132718Skan
15828132718Skan  /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
15829132718Skan     alternative.  */
15830169689Skan  cp_parser_decl_specifier_seq (parser,
15831169689Skan				CP_PARSER_FLAGS_OPTIONAL,
15832169689Skan				&decl_specifiers,
15833169689Skan				&declares_class_or_enum);
15834132718Skan  if (friend_p)
15835169689Skan    *friend_p = cp_parser_friend_p (&decl_specifiers);
15836161651Skan
15837161651Skan  /* There are no template typedefs.  */
15838169689Skan  if (decl_specifiers.specs[(int) ds_typedef])
15839161651Skan    {
15840169689Skan      error ("template declaration of %qs", "typedef");
15841161651Skan      decl = error_mark_node;
15842161651Skan    }
15843161651Skan
15844132718Skan  /* Gather up the access checks that occurred the
15845132718Skan     decl-specifier-seq.  */
15846132718Skan  stop_deferring_access_checks ();
15847132718Skan
15848132718Skan  /* Check for the declaration of a template class.  */
15849132718Skan  if (declares_class_or_enum)
15850132718Skan    {
15851132718Skan      if (cp_parser_declares_only_class_p (parser))
15852132718Skan	{
15853169689Skan	  decl = shadow_tag (&decl_specifiers);
15854169689Skan
15855169689Skan	  /* In this case:
15856169689Skan
15857169689Skan	       struct C {
15858169689Skan		 friend template <typename T> struct A<T>::B;
15859169689Skan	       };
15860169689Skan
15861169689Skan	     A<T>::B will be represented by a TYPENAME_TYPE, and
15862169689Skan	     therefore not recognized by shadow_tag.  */
15863169689Skan	  if (friend_p && *friend_p
15864169689Skan	      && !decl
15865169689Skan	      && decl_specifiers.type
15866169689Skan	      && TYPE_P (decl_specifiers.type))
15867169689Skan	    decl = decl_specifiers.type;
15868169689Skan
15869169689Skan	  if (decl && decl != error_mark_node)
15870132718Skan	    decl = TYPE_NAME (decl);
15871132718Skan	  else
15872132718Skan	    decl = error_mark_node;
15873169689Skan
15874169689Skan	  /* Perform access checks for template parameters.  */
15875169689Skan	  cp_parser_perform_template_parameter_access_checks (checks);
15876132718Skan	}
15877132718Skan    }
15878132718Skan  /* If it's not a template class, try for a template function.  If
15879132718Skan     the next token is a `;', then this declaration does not declare
15880132718Skan     anything.  But, if there were errors in the decl-specifiers, then
15881132718Skan     the error might well have come from an attempted class-specifier.
15882132718Skan     In that case, there's no need to warn about a missing declarator.  */
15883132718Skan  if (!decl
15884132718Skan      && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
15885169689Skan	  || decl_specifiers.type != error_mark_node))
15886169689Skan    decl = cp_parser_init_declarator (parser,
15887169689Skan				      &decl_specifiers,
15888169689Skan				      checks,
15889132718Skan				      /*function_definition_allowed_p=*/true,
15890132718Skan				      member_p,
15891132718Skan				      declares_class_or_enum,
15892132718Skan				      &function_definition_p);
15893132718Skan
15894132718Skan  pop_deferring_access_checks ();
15895132718Skan
15896132718Skan  /* Clear any current qualification; whatever comes next is the start
15897132718Skan     of something new.  */
15898132718Skan  parser->scope = NULL_TREE;
15899132718Skan  parser->qualifying_scope = NULL_TREE;
15900132718Skan  parser->object_scope = NULL_TREE;
15901132718Skan  /* Look for a trailing `;' after the declaration.  */
15902132718Skan  if (!function_definition_p
15903161651Skan      && (decl == error_mark_node
15904161651Skan	  || !cp_parser_require (parser, CPP_SEMICOLON, "`;'")))
15905132718Skan    cp_parser_skip_to_end_of_block_or_statement (parser);
15906132718Skan
15907132718Skan  return decl;
15908132718Skan}
15909132718Skan
15910132718Skan/* Parse a cast-expression that is not the operand of a unary "&".  */
15911132718Skan
15912132718Skanstatic tree
15913132718Skancp_parser_simple_cast_expression (cp_parser *parser)
15914132718Skan{
15915169689Skan  return cp_parser_cast_expression (parser, /*address_p=*/false,
15916169689Skan				    /*cast_p=*/false);
15917132718Skan}
15918132718Skan
15919132718Skan/* Parse a functional cast to TYPE.  Returns an expression
15920132718Skan   representing the cast.  */
15921132718Skan
15922132718Skanstatic tree
15923132718Skancp_parser_functional_cast (cp_parser* parser, tree type)
15924132718Skan{
15925132718Skan  tree expression_list;
15926132718Skan  tree cast;
15927132718Skan
15928132718Skan  expression_list
15929132718Skan    = cp_parser_parenthesized_expression_list (parser, false,
15930169689Skan					       /*cast_p=*/true,
15931132718Skan					       /*non_constant_p=*/NULL);
15932132718Skan
15933132718Skan  cast = build_functional_cast (type, expression_list);
15934132718Skan  /* [expr.const]/1: In an integral constant expression "only type
15935132718Skan     conversions to integral or enumeration type can be used".  */
15936161651Skan  if (TREE_CODE (type) == TYPE_DECL)
15937161651Skan    type = TREE_TYPE (type);
15938169689Skan  if (cast != error_mark_node
15939169689Skan      && !cast_valid_in_integral_constant_expression_p (type)
15940169689Skan      && (cp_parser_non_integral_constant_expression
15941169689Skan	  (parser, "a call to a constructor")))
15942169689Skan    return error_mark_node;
15943132718Skan  return cast;
15944132718Skan}
15945132718Skan
15946132718Skan/* Save the tokens that make up the body of a member function defined
15947132718Skan   in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
15948132718Skan   already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
15949132718Skan   specifiers applied to the declaration.  Returns the FUNCTION_DECL
15950132718Skan   for the member function.  */
15951132718Skan
15952132718Skanstatic tree
15953132718Skancp_parser_save_member_function_body (cp_parser* parser,
15954169689Skan				     cp_decl_specifier_seq *decl_specifiers,
15955169689Skan				     cp_declarator *declarator,
15956132718Skan				     tree attributes)
15957132718Skan{
15958169689Skan  cp_token *first;
15959169689Skan  cp_token *last;
15960132718Skan  tree fn;
15961132718Skan
15962132718Skan  /* Create the function-declaration.  */
15963132718Skan  fn = start_method (decl_specifiers, declarator, attributes);
15964132718Skan  /* If something went badly wrong, bail out now.  */
15965132718Skan  if (fn == error_mark_node)
15966132718Skan    {
15967132718Skan      /* If there's a function-body, skip it.  */
15968169689Skan      if (cp_parser_token_starts_function_definition_p
15969132718Skan	  (cp_lexer_peek_token (parser->lexer)))
15970132718Skan	cp_parser_skip_to_end_of_block_or_statement (parser);
15971132718Skan      return error_mark_node;
15972132718Skan    }
15973132718Skan
15974132718Skan  /* Remember it, if there default args to post process.  */
15975132718Skan  cp_parser_save_default_args (parser, fn);
15976132718Skan
15977169689Skan  /* Save away the tokens that make up the body of the
15978132718Skan     function.  */
15979169689Skan  first = parser->lexer->next_token;
15980169689Skan  cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
15981132718Skan  /* Handle function try blocks.  */
15982132718Skan  while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
15983169689Skan    cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
15984169689Skan  last = parser->lexer->next_token;
15985132718Skan
15986132718Skan  /* Save away the inline definition; we will process it when the
15987132718Skan     class is complete.  */
15988169689Skan  DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
15989132718Skan  DECL_PENDING_INLINE_P (fn) = 1;
15990132718Skan
15991132718Skan  /* We need to know that this was defined in the class, so that
15992132718Skan     friend templates are handled correctly.  */
15993132718Skan  DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
15994132718Skan
15995132718Skan  /* We're done with the inline definition.  */
15996132718Skan  finish_method (fn);
15997132718Skan
15998132718Skan  /* Add FN to the queue of functions to be parsed later.  */
15999132718Skan  TREE_VALUE (parser->unparsed_functions_queues)
16000169689Skan    = tree_cons (NULL_TREE, fn,
16001132718Skan		 TREE_VALUE (parser->unparsed_functions_queues));
16002132718Skan
16003132718Skan  return fn;
16004132718Skan}
16005132718Skan
16006132718Skan/* Parse a template-argument-list, as well as the trailing ">" (but
16007132718Skan   not the opening ">").  See cp_parser_template_argument_list for the
16008132718Skan   return value.  */
16009132718Skan
16010132718Skanstatic tree
16011132718Skancp_parser_enclosed_template_argument_list (cp_parser* parser)
16012132718Skan{
16013132718Skan  tree arguments;
16014132718Skan  tree saved_scope;
16015132718Skan  tree saved_qualifying_scope;
16016132718Skan  tree saved_object_scope;
16017132718Skan  bool saved_greater_than_is_operator_p;
16018169689Skan  bool saved_skip_evaluation;
16019132718Skan
16020132718Skan  /* [temp.names]
16021132718Skan
16022132718Skan     When parsing a template-id, the first non-nested `>' is taken as
16023132718Skan     the end of the template-argument-list rather than a greater-than
16024132718Skan     operator.  */
16025169689Skan  saved_greater_than_is_operator_p
16026132718Skan    = parser->greater_than_is_operator_p;
16027132718Skan  parser->greater_than_is_operator_p = false;
16028132718Skan  /* Parsing the argument list may modify SCOPE, so we save it
16029132718Skan     here.  */
16030132718Skan  saved_scope = parser->scope;
16031132718Skan  saved_qualifying_scope = parser->qualifying_scope;
16032132718Skan  saved_object_scope = parser->object_scope;
16033169689Skan  /* We need to evaluate the template arguments, even though this
16034169689Skan     template-id may be nested within a "sizeof".  */
16035169689Skan  saved_skip_evaluation = skip_evaluation;
16036169689Skan  skip_evaluation = false;
16037132718Skan  /* Parse the template-argument-list itself.  */
16038132718Skan  if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
16039132718Skan    arguments = NULL_TREE;
16040132718Skan  else
16041132718Skan    arguments = cp_parser_template_argument_list (parser);
16042132718Skan  /* Look for the `>' that ends the template-argument-list. If we find
16043132718Skan     a '>>' instead, it's probably just a typo.  */
16044132718Skan  if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
16045132718Skan    {
16046132718Skan      if (!saved_greater_than_is_operator_p)
16047132718Skan	{
16048169689Skan	  /* If we're in a nested template argument list, the '>>' has
16049169689Skan	    to be a typo for '> >'. We emit the error message, but we
16050169689Skan	    continue parsing and we push a '>' as next token, so that
16051169689Skan	    the argument list will be parsed correctly.  Note that the
16052169689Skan	    global source location is still on the token before the
16053169689Skan	    '>>', so we need to say explicitly where we want it.  */
16054169689Skan	  cp_token *token = cp_lexer_peek_token (parser->lexer);
16055169689Skan	  error ("%H%<>>%> should be %<> >%> "
16056169689Skan		 "within a nested template argument list",
16057169689Skan		 &token->location);
16058169689Skan
16059169689Skan	  /* ??? Proper recovery should terminate two levels of
16060169689Skan	     template argument list here.  */
16061132718Skan	  token->type = CPP_GREATER;
16062132718Skan	}
16063132718Skan      else
16064132718Skan	{
16065169689Skan	  /* If this is not a nested template argument list, the '>>'
16066169689Skan	    is a typo for '>'. Emit an error message and continue.
16067169689Skan	    Same deal about the token location, but here we can get it
16068169689Skan	    right by consuming the '>>' before issuing the diagnostic.  */
16069132718Skan	  cp_lexer_consume_token (parser->lexer);
16070169689Skan	  error ("spurious %<>>%>, use %<>%> to terminate "
16071169689Skan		 "a template argument list");
16072132718Skan	}
16073132718Skan    }
16074161651Skan  else
16075169689Skan    cp_parser_skip_to_end_of_template_parameter_list (parser);
16076132718Skan  /* The `>' token might be a greater-than operator again now.  */
16077169689Skan  parser->greater_than_is_operator_p
16078132718Skan    = saved_greater_than_is_operator_p;
16079132718Skan  /* Restore the SAVED_SCOPE.  */
16080132718Skan  parser->scope = saved_scope;
16081132718Skan  parser->qualifying_scope = saved_qualifying_scope;
16082132718Skan  parser->object_scope = saved_object_scope;
16083169689Skan  skip_evaluation = saved_skip_evaluation;
16084132718Skan
16085132718Skan  return arguments;
16086132718Skan}
16087132718Skan
16088132718Skan/* MEMBER_FUNCTION is a member function, or a friend.  If default
16089132718Skan   arguments, or the body of the function have not yet been parsed,
16090132718Skan   parse them now.  */
16091132718Skan
16092132718Skanstatic void
16093132718Skancp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
16094132718Skan{
16095132718Skan  /* If this member is a template, get the underlying
16096132718Skan     FUNCTION_DECL.  */
16097132718Skan  if (DECL_FUNCTION_TEMPLATE_P (member_function))
16098132718Skan    member_function = DECL_TEMPLATE_RESULT (member_function);
16099132718Skan
16100132718Skan  /* There should not be any class definitions in progress at this
16101132718Skan     point; the bodies of members are only parsed outside of all class
16102132718Skan     definitions.  */
16103169689Skan  gcc_assert (parser->num_classes_being_defined == 0);
16104132718Skan  /* While we're parsing the member functions we might encounter more
16105132718Skan     classes.  We want to handle them right away, but we don't want
16106132718Skan     them getting mixed up with functions that are currently in the
16107132718Skan     queue.  */
16108132718Skan  parser->unparsed_functions_queues
16109132718Skan    = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
16110132718Skan
16111132718Skan  /* Make sure that any template parameters are in scope.  */
16112132718Skan  maybe_begin_member_template_processing (member_function);
16113132718Skan
16114132718Skan  /* If the body of the function has not yet been parsed, parse it
16115132718Skan     now.  */
16116132718Skan  if (DECL_PENDING_INLINE_P (member_function))
16117132718Skan    {
16118132718Skan      tree function_scope;
16119132718Skan      cp_token_cache *tokens;
16120132718Skan
16121132718Skan      /* The function is no longer pending; we are processing it.  */
16122132718Skan      tokens = DECL_PENDING_INLINE_INFO (member_function);
16123132718Skan      DECL_PENDING_INLINE_INFO (member_function) = NULL;
16124132718Skan      DECL_PENDING_INLINE_P (member_function) = 0;
16125169689Skan
16126161651Skan      /* If this is a local class, enter the scope of the containing
16127161651Skan	 function.  */
16128161651Skan      function_scope = current_function_decl;
16129132718Skan      if (function_scope)
16130132718Skan	push_function_context_to (function_scope);
16131169689Skan
16132169689Skan
16133169689Skan      /* Push the body of the function onto the lexer stack.  */
16134169689Skan      cp_parser_push_lexer_for_tokens (parser, tokens);
16135169689Skan
16136132718Skan      /* Let the front end know that we going to be defining this
16137132718Skan	 function.  */
16138169689Skan      start_preparsed_function (member_function, NULL_TREE,
16139169689Skan				SF_PRE_PARSED | SF_INCLASS_INLINE);
16140169689Skan
16141169689Skan      /* Don't do access checking if it is a templated function.  */
16142169689Skan      if (processing_template_decl)
16143169689Skan	push_deferring_access_checks (dk_no_check);
16144169689Skan
16145132718Skan      /* Now, parse the body of the function.  */
16146132718Skan      cp_parser_function_definition_after_declarator (parser,
16147132718Skan						      /*inline_p=*/true);
16148169689Skan
16149169689Skan      if (processing_template_decl)
16150169689Skan	pop_deferring_access_checks ();
16151169689Skan
16152132718Skan      /* Leave the scope of the containing function.  */
16153132718Skan      if (function_scope)
16154132718Skan	pop_function_context_from (function_scope);
16155169689Skan      cp_parser_pop_lexer (parser);
16156132718Skan    }
16157132718Skan
16158132718Skan  /* Remove any template parameters from the symbol table.  */
16159132718Skan  maybe_end_member_template_processing ();
16160132718Skan
16161132718Skan  /* Restore the queue.  */
16162169689Skan  parser->unparsed_functions_queues
16163132718Skan    = TREE_CHAIN (parser->unparsed_functions_queues);
16164132718Skan}
16165132718Skan
16166132718Skan/* If DECL contains any default args, remember it on the unparsed
16167132718Skan   functions queue.  */
16168132718Skan
16169132718Skanstatic void
16170132718Skancp_parser_save_default_args (cp_parser* parser, tree decl)
16171132718Skan{
16172132718Skan  tree probe;
16173132718Skan
16174132718Skan  for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
16175132718Skan       probe;
16176132718Skan       probe = TREE_CHAIN (probe))
16177132718Skan    if (TREE_PURPOSE (probe))
16178132718Skan      {
16179132718Skan	TREE_PURPOSE (parser->unparsed_functions_queues)
16180169689Skan	  = tree_cons (current_class_type, decl,
16181132718Skan		       TREE_PURPOSE (parser->unparsed_functions_queues));
16182132718Skan	break;
16183132718Skan      }
16184132718Skan}
16185132718Skan
16186132718Skan/* FN is a FUNCTION_DECL which may contains a parameter with an
16187169689Skan   unparsed DEFAULT_ARG.  Parse the default args now.  This function
16188169689Skan   assumes that the current scope is the scope in which the default
16189169689Skan   argument should be processed.  */
16190132718Skan
16191132718Skanstatic void
16192132718Skancp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
16193132718Skan{
16194132718Skan  bool saved_local_variables_forbidden_p;
16195169689Skan  tree parm;
16196132718Skan
16197132718Skan  /* While we're parsing the default args, we might (due to the
16198132718Skan     statement expression extension) encounter more classes.  We want
16199132718Skan     to handle them right away, but we don't want them getting mixed
16200132718Skan     up with default args that are currently in the queue.  */
16201132718Skan  parser->unparsed_functions_queues
16202132718Skan    = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
16203132718Skan
16204169689Skan  /* Local variable names (and the `this' keyword) may not appear
16205169689Skan     in a default argument.  */
16206169689Skan  saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
16207169689Skan  parser->local_variables_forbidden_p = true;
16208169689Skan
16209169689Skan  for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
16210169689Skan       parm;
16211169689Skan       parm = TREE_CHAIN (parm))
16212132718Skan    {
16213169689Skan      cp_token_cache *tokens;
16214169689Skan      tree default_arg = TREE_PURPOSE (parm);
16215161651Skan      tree parsed_arg;
16216169689Skan      VEC(tree,gc) *insts;
16217169689Skan      tree copy;
16218169689Skan      unsigned ix;
16219161651Skan
16220161651Skan      if (!default_arg)
16221132718Skan	continue;
16222161651Skan
16223161651Skan      if (TREE_CODE (default_arg) != DEFAULT_ARG)
16224161651Skan	/* This can happen for a friend declaration for a function
16225161651Skan	   already declared with default arguments.  */
16226161651Skan	continue;
16227132718Skan
16228169689Skan       /* Push the saved tokens for the default argument onto the parser's
16229169689Skan	  lexer stack.  */
16230169689Skan      tokens = DEFARG_TOKENS (default_arg);
16231169689Skan      cp_parser_push_lexer_for_tokens (parser, tokens);
16232132718Skan
16233161651Skan      /* Parse the assignment-expression.  */
16234169689Skan      parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false);
16235169689Skan
16236169689Skan      if (!processing_template_decl)
16237169689Skan	parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
16238169689Skan
16239169689Skan      TREE_PURPOSE (parm) = parsed_arg;
16240169689Skan
16241161651Skan      /* Update any instantiations we've already created.  */
16242169689Skan      for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
16243169689Skan	   VEC_iterate (tree, insts, ix, copy); ix++)
16244169689Skan	TREE_PURPOSE (copy) = parsed_arg;
16245169689Skan
16246132718Skan      /* If the token stream has not been completely used up, then
16247132718Skan	 there was extra junk after the end of the default
16248132718Skan	 argument.  */
16249132718Skan      if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
16250169689Skan	cp_parser_error (parser, "expected %<,%>");
16251132718Skan
16252169689Skan      /* Revert to the main lexer.  */
16253169689Skan      cp_parser_pop_lexer (parser);
16254132718Skan    }
16255132718Skan
16256161651Skan  /* Make sure no default arg is missing.  */
16257161651Skan  check_default_args (fn);
16258161651Skan
16259169689Skan  /* Restore the state of local_variables_forbidden_p.  */
16260169689Skan  parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
16261169689Skan
16262132718Skan  /* Restore the queue.  */
16263169689Skan  parser->unparsed_functions_queues
16264132718Skan    = TREE_CHAIN (parser->unparsed_functions_queues);
16265132718Skan}
16266132718Skan
16267132718Skan/* Parse the operand of `sizeof' (or a similar operator).  Returns
16268132718Skan   either a TYPE or an expression, depending on the form of the
16269132718Skan   input.  The KEYWORD indicates which kind of expression we have
16270132718Skan   encountered.  */
16271132718Skan
16272132718Skanstatic tree
16273132718Skancp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
16274132718Skan{
16275132718Skan  static const char *format;
16276132718Skan  tree expr = NULL_TREE;
16277132718Skan  const char *saved_message;
16278132718Skan  bool saved_integral_constant_expression_p;
16279169689Skan  bool saved_non_integral_constant_expression_p;
16280132718Skan
16281132718Skan  /* Initialize FORMAT the first time we get here.  */
16282132718Skan  if (!format)
16283169689Skan    format = "types may not be defined in '%s' expressions";
16284132718Skan
16285132718Skan  /* Types cannot be defined in a `sizeof' expression.  Save away the
16286132718Skan     old message.  */
16287132718Skan  saved_message = parser->type_definition_forbidden_message;
16288132718Skan  /* And create the new one.  */
16289169689Skan  parser->type_definition_forbidden_message
16290169689Skan    = XNEWVEC (const char, strlen (format)
16291132718Skan	       + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
16292132718Skan	       + 1 /* `\0' */);
16293132718Skan  sprintf ((char *) parser->type_definition_forbidden_message,
16294132718Skan	   format, IDENTIFIER_POINTER (ridpointers[keyword]));
16295132718Skan
16296132718Skan  /* The restrictions on constant-expressions do not apply inside
16297132718Skan     sizeof expressions.  */
16298169689Skan  saved_integral_constant_expression_p
16299169689Skan    = parser->integral_constant_expression_p;
16300169689Skan  saved_non_integral_constant_expression_p
16301169689Skan    = parser->non_integral_constant_expression_p;
16302132718Skan  parser->integral_constant_expression_p = false;
16303132718Skan
16304132718Skan  /* Do not actually evaluate the expression.  */
16305132718Skan  ++skip_evaluation;
16306132718Skan  /* If it's a `(', then we might be looking at the type-id
16307132718Skan     construction.  */
16308132718Skan  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
16309132718Skan    {
16310132718Skan      tree type;
16311132718Skan      bool saved_in_type_id_in_expr_p;
16312132718Skan
16313132718Skan      /* We can't be sure yet whether we're looking at a type-id or an
16314132718Skan	 expression.  */
16315132718Skan      cp_parser_parse_tentatively (parser);
16316132718Skan      /* Consume the `('.  */
16317132718Skan      cp_lexer_consume_token (parser->lexer);
16318132718Skan      /* Parse the type-id.  */
16319132718Skan      saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
16320132718Skan      parser->in_type_id_in_expr_p = true;
16321132718Skan      type = cp_parser_type_id (parser);
16322132718Skan      parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
16323132718Skan      /* Now, look for the trailing `)'.  */
16324169689Skan      cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16325132718Skan      /* If all went well, then we're done.  */
16326132718Skan      if (cp_parser_parse_definitely (parser))
16327132718Skan	{
16328169689Skan	  cp_decl_specifier_seq decl_specs;
16329132718Skan
16330169689Skan	  /* Build a trivial decl-specifier-seq.  */
16331169689Skan	  clear_decl_specs (&decl_specs);
16332169689Skan	  decl_specs.type = type;
16333169689Skan
16334132718Skan	  /* Call grokdeclarator to figure out what type this is.  */
16335169689Skan	  expr = grokdeclarator (NULL,
16336169689Skan				 &decl_specs,
16337132718Skan				 TYPENAME,
16338132718Skan				 /*initialized=*/0,
16339132718Skan				 /*attrlist=*/NULL);
16340132718Skan	}
16341132718Skan    }
16342132718Skan
16343132718Skan  /* If the type-id production did not work out, then we must be
16344132718Skan     looking at the unary-expression production.  */
16345132718Skan  if (!expr)
16346169689Skan    expr = cp_parser_unary_expression (parser, /*address_p=*/false,
16347169689Skan				       /*cast_p=*/false);
16348132718Skan  /* Go back to evaluating expressions.  */
16349132718Skan  --skip_evaluation;
16350132718Skan
16351132718Skan  /* Free the message we created.  */
16352132718Skan  free ((char *) parser->type_definition_forbidden_message);
16353132718Skan  /* And restore the old one.  */
16354132718Skan  parser->type_definition_forbidden_message = saved_message;
16355169689Skan  parser->integral_constant_expression_p
16356169689Skan    = saved_integral_constant_expression_p;
16357169689Skan  parser->non_integral_constant_expression_p
16358169689Skan    = saved_non_integral_constant_expression_p;
16359132718Skan
16360132718Skan  return expr;
16361132718Skan}
16362132718Skan
16363132718Skan/* If the current declaration has no declarator, return true.  */
16364132718Skan
16365132718Skanstatic bool
16366132718Skancp_parser_declares_only_class_p (cp_parser *parser)
16367132718Skan{
16368169689Skan  /* If the next token is a `;' or a `,' then there is no
16369132718Skan     declarator.  */
16370132718Skan  return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
16371132718Skan	  || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
16372132718Skan}
16373132718Skan
16374169689Skan/* Update the DECL_SPECS to reflect the storage class indicated by
16375169689Skan   KEYWORD.  */
16376132718Skan
16377169689Skanstatic void
16378169689Skancp_parser_set_storage_class (cp_parser *parser,
16379169689Skan			     cp_decl_specifier_seq *decl_specs,
16380169689Skan			     enum rid keyword)
16381132718Skan{
16382169689Skan  cp_storage_class storage_class;
16383169689Skan
16384169689Skan  if (parser->in_unbraced_linkage_specification_p)
16385132718Skan    {
16386169689Skan      error ("invalid use of %qD in linkage specification",
16387169689Skan	     ridpointers[keyword]);
16388169689Skan      return;
16389169689Skan    }
16390169689Skan  else if (decl_specs->storage_class != sc_none)
16391169689Skan    {
16392169689Skan      decl_specs->conflicting_specifiers_p = true;
16393169689Skan      return;
16394169689Skan    }
16395132718Skan
16396169689Skan  if ((keyword == RID_EXTERN || keyword == RID_STATIC)
16397169689Skan      && decl_specs->specs[(int) ds_thread])
16398169689Skan    {
16399169689Skan      error ("%<__thread%> before %qD", ridpointers[keyword]);
16400169689Skan      decl_specs->specs[(int) ds_thread] = 0;
16401132718Skan    }
16402132718Skan
16403169689Skan  switch (keyword)
16404169689Skan    {
16405169689Skan    case RID_AUTO:
16406169689Skan      storage_class = sc_auto;
16407169689Skan      break;
16408169689Skan    case RID_REGISTER:
16409169689Skan      storage_class = sc_register;
16410169689Skan      break;
16411169689Skan    case RID_STATIC:
16412169689Skan      storage_class = sc_static;
16413169689Skan      break;
16414169689Skan    case RID_EXTERN:
16415169689Skan      storage_class = sc_extern;
16416169689Skan      break;
16417169689Skan    case RID_MUTABLE:
16418169689Skan      storage_class = sc_mutable;
16419169689Skan      break;
16420169689Skan    default:
16421169689Skan      gcc_unreachable ();
16422169689Skan    }
16423169689Skan  decl_specs->storage_class = storage_class;
16424169689Skan
16425169689Skan  /* A storage class specifier cannot be applied alongside a typedef
16426169689Skan     specifier. If there is a typedef specifier present then set
16427169689Skan     conflicting_specifiers_p which will trigger an error later
16428169689Skan     on in grokdeclarator. */
16429169689Skan  if (decl_specs->specs[(int)ds_typedef])
16430169689Skan    decl_specs->conflicting_specifiers_p = true;
16431132718Skan}
16432132718Skan
16433169689Skan/* Update the DECL_SPECS to reflect the TYPE_SPEC.  If USER_DEFINED_P
16434169689Skan   is true, the type is a user-defined type; otherwise it is a
16435169689Skan   built-in type specified by a keyword.  */
16436161651Skan
16437169689Skanstatic void
16438169689Skancp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
16439169689Skan			      tree type_spec,
16440169689Skan			      bool user_defined_p)
16441161651Skan{
16442169689Skan  decl_specs->any_specifiers_p = true;
16443169689Skan
16444169689Skan  /* If the user tries to redeclare bool or wchar_t (with, for
16445169689Skan     example, in "typedef int wchar_t;") we remember that this is what
16446169689Skan     happened.  In system headers, we ignore these declarations so
16447169689Skan     that G++ can work with system headers that are not C++-safe.  */
16448169689Skan  if (decl_specs->specs[(int) ds_typedef]
16449169689Skan      && !user_defined_p
16450169689Skan      && (type_spec == boolean_type_node
16451169689Skan	  || type_spec == wchar_type_node)
16452169689Skan      && (decl_specs->type
16453169689Skan	  || decl_specs->specs[(int) ds_long]
16454169689Skan	  || decl_specs->specs[(int) ds_short]
16455169689Skan	  || decl_specs->specs[(int) ds_unsigned]
16456169689Skan	  || decl_specs->specs[(int) ds_signed]))
16457161651Skan    {
16458169689Skan      decl_specs->redefined_builtin_type = type_spec;
16459169689Skan      if (!decl_specs->type)
16460169689Skan	{
16461169689Skan	  decl_specs->type = type_spec;
16462169689Skan	  decl_specs->user_defined_type_p = false;
16463169689Skan	}
16464161651Skan    }
16465169689Skan  else if (decl_specs->type)
16466169689Skan    decl_specs->multiple_types_p = true;
16467169689Skan  else
16468169689Skan    {
16469169689Skan      decl_specs->type = type_spec;
16470169689Skan      decl_specs->user_defined_type_p = user_defined_p;
16471169689Skan      decl_specs->redefined_builtin_type = NULL_TREE;
16472169689Skan    }
16473169689Skan}
16474161651Skan
16475169689Skan/* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
16476169689Skan   Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
16477169689Skan
16478169689Skanstatic bool
16479169689Skancp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
16480169689Skan{
16481169689Skan  return decl_specifiers->specs[(int) ds_friend] != 0;
16482161651Skan}
16483161651Skan
16484132718Skan/* If the next token is of the indicated TYPE, consume it.  Otherwise,
16485132718Skan   issue an error message indicating that TOKEN_DESC was expected.
16486169689Skan
16487132718Skan   Returns the token consumed, if the token had the appropriate type.
16488132718Skan   Otherwise, returns NULL.  */
16489132718Skan
16490132718Skanstatic cp_token *
16491132718Skancp_parser_require (cp_parser* parser,
16492169689Skan		   enum cpp_ttype type,
16493169689Skan		   const char* token_desc)
16494132718Skan{
16495132718Skan  if (cp_lexer_next_token_is (parser->lexer, type))
16496132718Skan    return cp_lexer_consume_token (parser->lexer);
16497132718Skan  else
16498132718Skan    {
16499132718Skan      /* Output the MESSAGE -- unless we're parsing tentatively.  */
16500132718Skan      if (!cp_parser_simulate_error (parser))
16501132718Skan	{
16502132718Skan	  char *message = concat ("expected ", token_desc, NULL);
16503132718Skan	  cp_parser_error (parser, message);
16504132718Skan	  free (message);
16505132718Skan	}
16506132718Skan      return NULL;
16507132718Skan    }
16508132718Skan}
16509132718Skan
16510169689Skan/* An error message is produced if the next token is not '>'.
16511169689Skan   All further tokens are skipped until the desired token is
16512169689Skan   found or '{', '}', ';' or an unbalanced ')' or ']'.  */
16513132718Skan
16514132718Skanstatic void
16515169689Skancp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
16516132718Skan{
16517169689Skan  /* Current level of '< ... >'.  */
16518169689Skan  unsigned level = 0;
16519169689Skan  /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'.  */
16520132718Skan  unsigned nesting_depth = 0;
16521132718Skan
16522169689Skan  /* Are we ready, yet?  If not, issue error message.  */
16523169689Skan  if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
16524132718Skan    return;
16525132718Skan
16526132718Skan  /* Skip tokens until the desired token is found.  */
16527132718Skan  while (true)
16528132718Skan    {
16529132718Skan      /* Peek at the next token.  */
16530169689Skan      switch (cp_lexer_peek_token (parser->lexer)->type)
16531132718Skan	{
16532169689Skan	case CPP_LESS:
16533169689Skan	  if (!nesting_depth)
16534169689Skan	    ++level;
16535169689Skan	  break;
16536169689Skan
16537169689Skan	case CPP_GREATER:
16538169689Skan	  if (!nesting_depth && level-- == 0)
16539169689Skan	    {
16540169689Skan	      /* We've reached the token we want, consume it and stop.  */
16541169689Skan	      cp_lexer_consume_token (parser->lexer);
16542169689Skan	      return;
16543169689Skan	    }
16544169689Skan	  break;
16545169689Skan
16546169689Skan	case CPP_OPEN_PAREN:
16547169689Skan	case CPP_OPEN_SQUARE:
16548169689Skan	  ++nesting_depth;
16549169689Skan	  break;
16550169689Skan
16551169689Skan	case CPP_CLOSE_PAREN:
16552169689Skan	case CPP_CLOSE_SQUARE:
16553132718Skan	  if (nesting_depth-- == 0)
16554132718Skan	    return;
16555169689Skan	  break;
16556169689Skan
16557169689Skan	case CPP_EOF:
16558169689Skan	case CPP_PRAGMA_EOL:
16559169689Skan	case CPP_SEMICOLON:
16560169689Skan	case CPP_OPEN_BRACE:
16561169689Skan	case CPP_CLOSE_BRACE:
16562169689Skan	  /* The '>' was probably forgotten, don't look further.  */
16563169689Skan	  return;
16564169689Skan
16565169689Skan	default:
16566169689Skan	  break;
16567132718Skan	}
16568169689Skan
16569132718Skan      /* Consume this token.  */
16570132718Skan      cp_lexer_consume_token (parser->lexer);
16571132718Skan    }
16572132718Skan}
16573132718Skan
16574132718Skan/* If the next token is the indicated keyword, consume it.  Otherwise,
16575132718Skan   issue an error message indicating that TOKEN_DESC was expected.
16576169689Skan
16577132718Skan   Returns the token consumed, if the token had the appropriate type.
16578132718Skan   Otherwise, returns NULL.  */
16579132718Skan
16580132718Skanstatic cp_token *
16581132718Skancp_parser_require_keyword (cp_parser* parser,
16582169689Skan			   enum rid keyword,
16583169689Skan			   const char* token_desc)
16584132718Skan{
16585132718Skan  cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
16586132718Skan
16587132718Skan  if (token && token->keyword != keyword)
16588132718Skan    {
16589132718Skan      dyn_string_t error_msg;
16590132718Skan
16591132718Skan      /* Format the error message.  */
16592132718Skan      error_msg = dyn_string_new (0);
16593132718Skan      dyn_string_append_cstr (error_msg, "expected ");
16594132718Skan      dyn_string_append_cstr (error_msg, token_desc);
16595132718Skan      cp_parser_error (parser, error_msg->s);
16596132718Skan      dyn_string_delete (error_msg);
16597132718Skan      return NULL;
16598132718Skan    }
16599132718Skan
16600132718Skan  return token;
16601132718Skan}
16602132718Skan
16603132718Skan/* Returns TRUE iff TOKEN is a token that can begin the body of a
16604132718Skan   function-definition.  */
16605132718Skan
16606169689Skanstatic bool
16607132718Skancp_parser_token_starts_function_definition_p (cp_token* token)
16608132718Skan{
16609132718Skan  return (/* An ordinary function-body begins with an `{'.  */
16610132718Skan	  token->type == CPP_OPEN_BRACE
16611132718Skan	  /* A ctor-initializer begins with a `:'.  */
16612132718Skan	  || token->type == CPP_COLON
16613132718Skan	  /* A function-try-block begins with `try'.  */
16614132718Skan	  || token->keyword == RID_TRY
16615132718Skan	  /* The named return value extension begins with `return'.  */
16616132718Skan	  || token->keyword == RID_RETURN);
16617132718Skan}
16618132718Skan
16619132718Skan/* Returns TRUE iff the next token is the ":" or "{" beginning a class
16620132718Skan   definition.  */
16621132718Skan
16622132718Skanstatic bool
16623132718Skancp_parser_next_token_starts_class_definition_p (cp_parser *parser)
16624132718Skan{
16625132718Skan  cp_token *token;
16626132718Skan
16627132718Skan  token = cp_lexer_peek_token (parser->lexer);
16628132718Skan  return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
16629132718Skan}
16630132718Skan
16631132718Skan/* Returns TRUE iff the next token is the "," or ">" ending a
16632169689Skan   template-argument.  */
16633132718Skan
16634132718Skanstatic bool
16635132718Skancp_parser_next_token_ends_template_argument_p (cp_parser *parser)
16636132718Skan{
16637132718Skan  cp_token *token;
16638132718Skan
16639132718Skan  token = cp_lexer_peek_token (parser->lexer);
16640146895Skan  return (token->type == CPP_COMMA || token->type == CPP_GREATER);
16641132718Skan}
16642132718Skan
16643169689Skan/* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
16644132718Skan   (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
16645132718Skan
16646132718Skanstatic bool
16647169689Skancp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
16648132718Skan						     size_t n)
16649132718Skan{
16650132718Skan  cp_token *token;
16651132718Skan
16652132718Skan  token = cp_lexer_peek_nth_token (parser->lexer, n);
16653132718Skan  if (token->type == CPP_LESS)
16654132718Skan    return true;
16655132718Skan  /* Check for the sequence `<::' in the original code. It would be lexed as
16656132718Skan     `[:', where `[' is a digraph, and there is no whitespace before
16657132718Skan     `:'.  */
16658132718Skan  if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
16659132718Skan    {
16660132718Skan      cp_token *token2;
16661132718Skan      token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
16662132718Skan      if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
16663132718Skan	return true;
16664132718Skan    }
16665132718Skan  return false;
16666132718Skan}
16667169689Skan
16668132718Skan/* Returns the kind of tag indicated by TOKEN, if it is a class-key,
16669132718Skan   or none_type otherwise.  */
16670132718Skan
16671132718Skanstatic enum tag_types
16672132718Skancp_parser_token_is_class_key (cp_token* token)
16673132718Skan{
16674132718Skan  switch (token->keyword)
16675132718Skan    {
16676132718Skan    case RID_CLASS:
16677132718Skan      return class_type;
16678132718Skan    case RID_STRUCT:
16679132718Skan      return record_type;
16680132718Skan    case RID_UNION:
16681132718Skan      return union_type;
16682169689Skan
16683132718Skan    default:
16684132718Skan      return none_type;
16685132718Skan    }
16686132718Skan}
16687132718Skan
16688132718Skan/* Issue an error message if the CLASS_KEY does not match the TYPE.  */
16689132718Skan
16690132718Skanstatic void
16691132718Skancp_parser_check_class_key (enum tag_types class_key, tree type)
16692132718Skan{
16693132718Skan  if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
16694169689Skan    pedwarn ("%qs tag used in naming %q#T",
16695132718Skan	    class_key == union_type ? "union"
16696169689Skan	     : class_key == record_type ? "struct" : "class",
16697132718Skan	     type);
16698132718Skan}
16699169689Skan
16700132718Skan/* Issue an error message if DECL is redeclared with different
16701132718Skan   access than its original declaration [class.access.spec/3].
16702132718Skan   This applies to nested classes and nested class templates.
16703132718Skan   [class.mem/1].  */
16704132718Skan
16705169689Skanstatic void
16706169689Skancp_parser_check_access_in_redeclaration (tree decl)
16707132718Skan{
16708132718Skan  if (!CLASS_TYPE_P (TREE_TYPE (decl)))
16709132718Skan    return;
16710132718Skan
16711132718Skan  if ((TREE_PRIVATE (decl)
16712132718Skan       != (current_access_specifier == access_private_node))
16713132718Skan      || (TREE_PROTECTED (decl)
16714132718Skan	  != (current_access_specifier == access_protected_node)))
16715169689Skan    error ("%qD redeclared with different access", decl);
16716132718Skan}
16717132718Skan
16718132718Skan/* Look for the `template' keyword, as a syntactic disambiguator.
16719169689Skan   Return TRUE iff it is present, in which case it will be
16720132718Skan   consumed.  */
16721132718Skan
16722132718Skanstatic bool
16723132718Skancp_parser_optional_template_keyword (cp_parser *parser)
16724132718Skan{
16725132718Skan  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16726132718Skan    {
16727132718Skan      /* The `template' keyword can only be used within templates;
16728132718Skan	 outside templates the parser can always figure out what is a
16729132718Skan	 template and what is not.  */
16730132718Skan      if (!processing_template_decl)
16731132718Skan	{
16732169689Skan	  error ("%<template%> (as a disambiguator) is only allowed "
16733132718Skan		 "within templates");
16734132718Skan	  /* If this part of the token stream is rescanned, the same
16735132718Skan	     error message would be generated.  So, we purge the token
16736132718Skan	     from the stream.  */
16737132718Skan	  cp_lexer_purge_token (parser->lexer);
16738132718Skan	  return false;
16739132718Skan	}
16740132718Skan      else
16741132718Skan	{
16742132718Skan	  /* Consume the `template' keyword.  */
16743132718Skan	  cp_lexer_consume_token (parser->lexer);
16744132718Skan	  return true;
16745132718Skan	}
16746132718Skan    }
16747132718Skan
16748132718Skan  return false;
16749132718Skan}
16750132718Skan
16751132718Skan/* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
16752132718Skan   set PARSER->SCOPE, and perform other related actions.  */
16753132718Skan
16754132718Skanstatic void
16755132718Skancp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
16756132718Skan{
16757169689Skan  int i;
16758169689Skan  struct tree_check *check_value;
16759169689Skan  deferred_access_check *chk;
16760169689Skan  VEC (deferred_access_check,gc) *checks;
16761132718Skan
16762132718Skan  /* Get the stored value.  */
16763169689Skan  check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
16764132718Skan  /* Perform any access checks that were deferred.  */
16765169689Skan  checks = check_value->checks;
16766169689Skan  if (checks)
16767169689Skan    {
16768169689Skan      for (i = 0 ;
16769169689Skan	   VEC_iterate (deferred_access_check, checks, i, chk) ;
16770169689Skan	   ++i)
16771169689Skan	{
16772169689Skan	  perform_or_defer_access_check (chk->binfo,
16773169689Skan					 chk->decl,
16774169689Skan					 chk->diag_decl);
16775169689Skan	}
16776169689Skan    }
16777132718Skan  /* Set the scope from the stored value.  */
16778169689Skan  parser->scope = check_value->value;
16779169689Skan  parser->qualifying_scope = check_value->qualifying_scope;
16780132718Skan  parser->object_scope = NULL_TREE;
16781132718Skan}
16782132718Skan
16783169689Skan/* Consume tokens up through a non-nested END token.  */
16784132718Skan
16785132718Skanstatic void
16786169689Skancp_parser_cache_group (cp_parser *parser,
16787132718Skan		       enum cpp_ttype end,
16788132718Skan		       unsigned depth)
16789132718Skan{
16790132718Skan  while (true)
16791132718Skan    {
16792132718Skan      cp_token *token;
16793132718Skan
16794132718Skan      /* Abort a parenthesized expression if we encounter a brace.  */
16795132718Skan      if ((end == CPP_CLOSE_PAREN || depth == 0)
16796132718Skan	  && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16797132718Skan	return;
16798132718Skan      /* If we've reached the end of the file, stop.  */
16799169689Skan      if (cp_lexer_next_token_is (parser->lexer, CPP_EOF)
16800169689Skan	  || (end != CPP_PRAGMA_EOL
16801169689Skan	      && cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL)))
16802132718Skan	return;
16803132718Skan      /* Consume the next token.  */
16804132718Skan      token = cp_lexer_consume_token (parser->lexer);
16805132718Skan      /* See if it starts a new group.  */
16806132718Skan      if (token->type == CPP_OPEN_BRACE)
16807132718Skan	{
16808169689Skan	  cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
16809132718Skan	  if (depth == 0)
16810132718Skan	    return;
16811132718Skan	}
16812132718Skan      else if (token->type == CPP_OPEN_PAREN)
16813169689Skan	cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
16814169689Skan      else if (token->type == CPP_PRAGMA)
16815169689Skan	cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
16816132718Skan      else if (token->type == end)
16817132718Skan	return;
16818132718Skan    }
16819132718Skan}
16820132718Skan
16821132718Skan/* Begin parsing tentatively.  We always save tokens while parsing
16822132718Skan   tentatively so that if the tentative parsing fails we can restore the
16823132718Skan   tokens.  */
16824132718Skan
16825132718Skanstatic void
16826132718Skancp_parser_parse_tentatively (cp_parser* parser)
16827132718Skan{
16828132718Skan  /* Enter a new parsing context.  */
16829132718Skan  parser->context = cp_parser_context_new (parser->context);
16830132718Skan  /* Begin saving tokens.  */
16831132718Skan  cp_lexer_save_tokens (parser->lexer);
16832132718Skan  /* In order to avoid repetitive access control error messages,
16833132718Skan     access checks are queued up until we are no longer parsing
16834132718Skan     tentatively.  */
16835132718Skan  push_deferring_access_checks (dk_deferred);
16836132718Skan}
16837132718Skan
16838132718Skan/* Commit to the currently active tentative parse.  */
16839132718Skan
16840132718Skanstatic void
16841132718Skancp_parser_commit_to_tentative_parse (cp_parser* parser)
16842132718Skan{
16843132718Skan  cp_parser_context *context;
16844132718Skan  cp_lexer *lexer;
16845132718Skan
16846132718Skan  /* Mark all of the levels as committed.  */
16847132718Skan  lexer = parser->lexer;
16848132718Skan  for (context = parser->context; context->next; context = context->next)
16849132718Skan    {
16850132718Skan      if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
16851132718Skan	break;
16852132718Skan      context->status = CP_PARSER_STATUS_KIND_COMMITTED;
16853132718Skan      while (!cp_lexer_saving_tokens (lexer))
16854132718Skan	lexer = lexer->next;
16855132718Skan      cp_lexer_commit_tokens (lexer);
16856132718Skan    }
16857132718Skan}
16858132718Skan
16859132718Skan/* Abort the currently active tentative parse.  All consumed tokens
16860132718Skan   will be rolled back, and no diagnostics will be issued.  */
16861132718Skan
16862132718Skanstatic void
16863132718Skancp_parser_abort_tentative_parse (cp_parser* parser)
16864132718Skan{
16865132718Skan  cp_parser_simulate_error (parser);
16866132718Skan  /* Now, pretend that we want to see if the construct was
16867132718Skan     successfully parsed.  */
16868132718Skan  cp_parser_parse_definitely (parser);
16869132718Skan}
16870132718Skan
16871132718Skan/* Stop parsing tentatively.  If a parse error has occurred, restore the
16872132718Skan   token stream.  Otherwise, commit to the tokens we have consumed.
16873132718Skan   Returns true if no error occurred; false otherwise.  */
16874132718Skan
16875132718Skanstatic bool
16876132718Skancp_parser_parse_definitely (cp_parser* parser)
16877132718Skan{
16878132718Skan  bool error_occurred;
16879132718Skan  cp_parser_context *context;
16880132718Skan
16881132718Skan  /* Remember whether or not an error occurred, since we are about to
16882132718Skan     destroy that information.  */
16883132718Skan  error_occurred = cp_parser_error_occurred (parser);
16884132718Skan  /* Remove the topmost context from the stack.  */
16885132718Skan  context = parser->context;
16886132718Skan  parser->context = context->next;
16887132718Skan  /* If no parse errors occurred, commit to the tentative parse.  */
16888132718Skan  if (!error_occurred)
16889132718Skan    {
16890132718Skan      /* Commit to the tokens read tentatively, unless that was
16891132718Skan	 already done.  */
16892132718Skan      if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
16893132718Skan	cp_lexer_commit_tokens (parser->lexer);
16894132718Skan
16895132718Skan      pop_to_parent_deferring_access_checks ();
16896132718Skan    }
16897132718Skan  /* Otherwise, if errors occurred, roll back our state so that things
16898132718Skan     are just as they were before we began the tentative parse.  */
16899132718Skan  else
16900132718Skan    {
16901132718Skan      cp_lexer_rollback_tokens (parser->lexer);
16902132718Skan      pop_deferring_access_checks ();
16903132718Skan    }
16904132718Skan  /* Add the context to the front of the free list.  */
16905132718Skan  context->next = cp_parser_context_free_list;
16906132718Skan  cp_parser_context_free_list = context;
16907132718Skan
16908132718Skan  return !error_occurred;
16909132718Skan}
16910132718Skan
16911169689Skan/* Returns true if we are parsing tentatively and are not committed to
16912169689Skan   this tentative parse.  */
16913132718Skan
16914132718Skanstatic bool
16915169689Skancp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
16916132718Skan{
16917132718Skan  return (cp_parser_parsing_tentatively (parser)
16918169689Skan	  && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
16919132718Skan}
16920132718Skan
16921132718Skan/* Returns nonzero iff an error has occurred during the most recent
16922132718Skan   tentative parse.  */
16923169689Skan
16924132718Skanstatic bool
16925132718Skancp_parser_error_occurred (cp_parser* parser)
16926132718Skan{
16927132718Skan  return (cp_parser_parsing_tentatively (parser)
16928132718Skan	  && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
16929132718Skan}
16930132718Skan
16931132718Skan/* Returns nonzero if GNU extensions are allowed.  */
16932132718Skan
16933132718Skanstatic bool
16934132718Skancp_parser_allow_gnu_extensions_p (cp_parser* parser)
16935132718Skan{
16936132718Skan  return parser->allow_gnu_extensions_p;
16937132718Skan}
16938169689Skan
16939169689Skan/* Objective-C++ Productions */
16940132718Skan
16941169689Skan
16942169689Skan/* Parse an Objective-C expression, which feeds into a primary-expression
16943169689Skan   above.
16944169689Skan
16945169689Skan   objc-expression:
16946169689Skan     objc-message-expression
16947169689Skan     objc-string-literal
16948169689Skan     objc-encode-expression
16949169689Skan     objc-protocol-expression
16950169689Skan     objc-selector-expression
16951169689Skan
16952169689Skan  Returns a tree representation of the expression.  */
16953169689Skan
16954169689Skanstatic tree
16955169689Skancp_parser_objc_expression (cp_parser* parser)
16956169689Skan{
16957169689Skan  /* Try to figure out what kind of declaration is present.  */
16958169689Skan  cp_token *kwd = cp_lexer_peek_token (parser->lexer);
16959169689Skan
16960169689Skan  switch (kwd->type)
16961169689Skan    {
16962169689Skan    case CPP_OPEN_SQUARE:
16963169689Skan      return cp_parser_objc_message_expression (parser);
16964169689Skan
16965169689Skan    case CPP_OBJC_STRING:
16966169689Skan      kwd = cp_lexer_consume_token (parser->lexer);
16967169689Skan      return objc_build_string_object (kwd->u.value);
16968169689Skan
16969169689Skan    case CPP_KEYWORD:
16970169689Skan      switch (kwd->keyword)
16971169689Skan	{
16972169689Skan	case RID_AT_ENCODE:
16973169689Skan	  return cp_parser_objc_encode_expression (parser);
16974169689Skan
16975169689Skan	case RID_AT_PROTOCOL:
16976169689Skan	  return cp_parser_objc_protocol_expression (parser);
16977169689Skan
16978169689Skan	case RID_AT_SELECTOR:
16979169689Skan	  return cp_parser_objc_selector_expression (parser);
16980169689Skan
16981169689Skan	default:
16982169689Skan	  break;
16983169689Skan	}
16984169689Skan    default:
16985169689Skan      error ("misplaced %<@%D%> Objective-C++ construct", kwd->u.value);
16986169689Skan      cp_parser_skip_to_end_of_block_or_statement (parser);
16987169689Skan    }
16988169689Skan
16989169689Skan  return error_mark_node;
16990169689Skan}
16991169689Skan
16992169689Skan/* Parse an Objective-C message expression.
16993169689Skan
16994169689Skan   objc-message-expression:
16995169689Skan     [ objc-message-receiver objc-message-args ]
16996169689Skan
16997169689Skan   Returns a representation of an Objective-C message.  */
16998169689Skan
16999169689Skanstatic tree
17000169689Skancp_parser_objc_message_expression (cp_parser* parser)
17001169689Skan{
17002169689Skan  tree receiver, messageargs;
17003169689Skan
17004169689Skan  cp_lexer_consume_token (parser->lexer);  /* Eat '['.  */
17005169689Skan  receiver = cp_parser_objc_message_receiver (parser);
17006169689Skan  messageargs = cp_parser_objc_message_args (parser);
17007169689Skan  cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
17008169689Skan
17009169689Skan  return objc_build_message_expr (build_tree_list (receiver, messageargs));
17010169689Skan}
17011169689Skan
17012169689Skan/* Parse an objc-message-receiver.
17013169689Skan
17014169689Skan   objc-message-receiver:
17015169689Skan     expression
17016169689Skan     simple-type-specifier
17017169689Skan
17018169689Skan  Returns a representation of the type or expression.  */
17019169689Skan
17020169689Skanstatic tree
17021169689Skancp_parser_objc_message_receiver (cp_parser* parser)
17022169689Skan{
17023169689Skan  tree rcv;
17024169689Skan
17025169689Skan  /* An Objective-C message receiver may be either (1) a type
17026169689Skan     or (2) an expression.  */
17027169689Skan  cp_parser_parse_tentatively (parser);
17028169689Skan  rcv = cp_parser_expression (parser, false);
17029169689Skan
17030169689Skan  if (cp_parser_parse_definitely (parser))
17031169689Skan    return rcv;
17032169689Skan
17033169689Skan  rcv = cp_parser_simple_type_specifier (parser,
17034169689Skan					 /*decl_specs=*/NULL,
17035169689Skan					 CP_PARSER_FLAGS_NONE);
17036169689Skan
17037169689Skan  return objc_get_class_reference (rcv);
17038169689Skan}
17039169689Skan
17040169689Skan/* Parse the arguments and selectors comprising an Objective-C message.
17041169689Skan
17042169689Skan   objc-message-args:
17043169689Skan     objc-selector
17044169689Skan     objc-selector-args
17045169689Skan     objc-selector-args , objc-comma-args
17046169689Skan
17047169689Skan   objc-selector-args:
17048169689Skan     objc-selector [opt] : assignment-expression
17049169689Skan     objc-selector-args objc-selector [opt] : assignment-expression
17050169689Skan
17051169689Skan   objc-comma-args:
17052169689Skan     assignment-expression
17053169689Skan     objc-comma-args , assignment-expression
17054169689Skan
17055169689Skan   Returns a TREE_LIST, with TREE_PURPOSE containing a list of
17056169689Skan   selector arguments and TREE_VALUE containing a list of comma
17057169689Skan   arguments.  */
17058169689Skan
17059169689Skanstatic tree
17060169689Skancp_parser_objc_message_args (cp_parser* parser)
17061169689Skan{
17062169689Skan  tree sel_args = NULL_TREE, addl_args = NULL_TREE;
17063169689Skan  bool maybe_unary_selector_p = true;
17064169689Skan  cp_token *token = cp_lexer_peek_token (parser->lexer);
17065169689Skan
17066169689Skan  while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
17067169689Skan    {
17068169689Skan      tree selector = NULL_TREE, arg;
17069169689Skan
17070169689Skan      if (token->type != CPP_COLON)
17071169689Skan	selector = cp_parser_objc_selector (parser);
17072169689Skan
17073169689Skan      /* Detect if we have a unary selector.  */
17074169689Skan      if (maybe_unary_selector_p
17075169689Skan	  && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
17076169689Skan	return build_tree_list (selector, NULL_TREE);
17077169689Skan
17078169689Skan      maybe_unary_selector_p = false;
17079169689Skan      cp_parser_require (parser, CPP_COLON, "`:'");
17080169689Skan      arg = cp_parser_assignment_expression (parser, false);
17081169689Skan
17082169689Skan      sel_args
17083169689Skan	= chainon (sel_args,
17084169689Skan		   build_tree_list (selector, arg));
17085169689Skan
17086169689Skan      token = cp_lexer_peek_token (parser->lexer);
17087169689Skan    }
17088169689Skan
17089169689Skan  /* Handle non-selector arguments, if any. */
17090169689Skan  while (token->type == CPP_COMMA)
17091169689Skan    {
17092169689Skan      tree arg;
17093169689Skan
17094169689Skan      cp_lexer_consume_token (parser->lexer);
17095169689Skan      arg = cp_parser_assignment_expression (parser, false);
17096169689Skan
17097169689Skan      addl_args
17098169689Skan	= chainon (addl_args,
17099169689Skan		   build_tree_list (NULL_TREE, arg));
17100169689Skan
17101169689Skan      token = cp_lexer_peek_token (parser->lexer);
17102169689Skan    }
17103169689Skan
17104169689Skan  return build_tree_list (sel_args, addl_args);
17105169689Skan}
17106169689Skan
17107169689Skan/* Parse an Objective-C encode expression.
17108169689Skan
17109169689Skan   objc-encode-expression:
17110169689Skan     @encode objc-typename
17111169689Skan
17112169689Skan   Returns an encoded representation of the type argument.  */
17113169689Skan
17114169689Skanstatic tree
17115169689Skancp_parser_objc_encode_expression (cp_parser* parser)
17116169689Skan{
17117169689Skan  tree type;
17118169689Skan
17119169689Skan  cp_lexer_consume_token (parser->lexer);  /* Eat '@encode'.  */
17120169689Skan  cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17121169689Skan  type = complete_type (cp_parser_type_id (parser));
17122169689Skan  cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17123169689Skan
17124169689Skan  if (!type)
17125169689Skan    {
17126169689Skan      error ("%<@encode%> must specify a type as an argument");
17127169689Skan      return error_mark_node;
17128169689Skan    }
17129169689Skan
17130169689Skan  return objc_build_encode_expr (type);
17131169689Skan}
17132169689Skan
17133169689Skan/* Parse an Objective-C @defs expression.  */
17134169689Skan
17135169689Skanstatic tree
17136169689Skancp_parser_objc_defs_expression (cp_parser *parser)
17137169689Skan{
17138169689Skan  tree name;
17139169689Skan
17140169689Skan  cp_lexer_consume_token (parser->lexer);  /* Eat '@defs'.  */
17141169689Skan  cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17142169689Skan  name = cp_parser_identifier (parser);
17143169689Skan  cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17144169689Skan
17145169689Skan  return objc_get_class_ivars (name);
17146169689Skan}
17147169689Skan
17148169689Skan/* Parse an Objective-C protocol expression.
17149169689Skan
17150169689Skan  objc-protocol-expression:
17151169689Skan    @protocol ( identifier )
17152169689Skan
17153169689Skan  Returns a representation of the protocol expression.  */
17154169689Skan
17155169689Skanstatic tree
17156169689Skancp_parser_objc_protocol_expression (cp_parser* parser)
17157169689Skan{
17158169689Skan  tree proto;
17159169689Skan
17160169689Skan  cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
17161169689Skan  cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17162169689Skan  proto = cp_parser_identifier (parser);
17163169689Skan  cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17164169689Skan
17165169689Skan  return objc_build_protocol_expr (proto);
17166169689Skan}
17167169689Skan
17168169689Skan/* Parse an Objective-C selector expression.
17169169689Skan
17170169689Skan   objc-selector-expression:
17171169689Skan     @selector ( objc-method-signature )
17172169689Skan
17173169689Skan   objc-method-signature:
17174169689Skan     objc-selector
17175169689Skan     objc-selector-seq
17176169689Skan
17177169689Skan   objc-selector-seq:
17178169689Skan     objc-selector :
17179169689Skan     objc-selector-seq objc-selector :
17180169689Skan
17181169689Skan  Returns a representation of the method selector.  */
17182169689Skan
17183169689Skanstatic tree
17184169689Skancp_parser_objc_selector_expression (cp_parser* parser)
17185169689Skan{
17186169689Skan  tree sel_seq = NULL_TREE;
17187169689Skan  bool maybe_unary_selector_p = true;
17188169689Skan  cp_token *token;
17189169689Skan
17190169689Skan  cp_lexer_consume_token (parser->lexer);  /* Eat '@selector'.  */
17191169689Skan  cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17192169689Skan  token = cp_lexer_peek_token (parser->lexer);
17193169689Skan
17194169689Skan  while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
17195169689Skan	 || token->type == CPP_SCOPE)
17196169689Skan    {
17197169689Skan      tree selector = NULL_TREE;
17198169689Skan
17199169689Skan      if (token->type != CPP_COLON
17200169689Skan	  || token->type == CPP_SCOPE)
17201169689Skan	selector = cp_parser_objc_selector (parser);
17202169689Skan
17203169689Skan      if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
17204169689Skan	  && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
17205169689Skan	{
17206169689Skan	  /* Detect if we have a unary selector.  */
17207169689Skan	  if (maybe_unary_selector_p)
17208169689Skan	    {
17209169689Skan	      sel_seq = selector;
17210169689Skan	      goto finish_selector;
17211169689Skan	    }
17212169689Skan	  else
17213169689Skan	    {
17214169689Skan	      cp_parser_error (parser, "expected %<:%>");
17215169689Skan	    }
17216169689Skan	}
17217169689Skan      maybe_unary_selector_p = false;
17218169689Skan      token = cp_lexer_consume_token (parser->lexer);
17219169689Skan
17220169689Skan      if (token->type == CPP_SCOPE)
17221169689Skan	{
17222169689Skan	  sel_seq
17223169689Skan	    = chainon (sel_seq,
17224169689Skan		       build_tree_list (selector, NULL_TREE));
17225169689Skan	  sel_seq
17226169689Skan	    = chainon (sel_seq,
17227169689Skan		       build_tree_list (NULL_TREE, NULL_TREE));
17228169689Skan	}
17229169689Skan      else
17230169689Skan	sel_seq
17231169689Skan	  = chainon (sel_seq,
17232169689Skan		     build_tree_list (selector, NULL_TREE));
17233169689Skan
17234169689Skan      token = cp_lexer_peek_token (parser->lexer);
17235169689Skan    }
17236169689Skan
17237169689Skan finish_selector:
17238169689Skan  cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17239169689Skan
17240169689Skan  return objc_build_selector_expr (sel_seq);
17241169689Skan}
17242169689Skan
17243169689Skan/* Parse a list of identifiers.
17244169689Skan
17245169689Skan   objc-identifier-list:
17246169689Skan     identifier
17247169689Skan     objc-identifier-list , identifier
17248169689Skan
17249169689Skan   Returns a TREE_LIST of identifier nodes.  */
17250169689Skan
17251169689Skanstatic tree
17252169689Skancp_parser_objc_identifier_list (cp_parser* parser)
17253169689Skan{
17254169689Skan  tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
17255169689Skan  cp_token *sep = cp_lexer_peek_token (parser->lexer);
17256169689Skan
17257169689Skan  while (sep->type == CPP_COMMA)
17258169689Skan    {
17259169689Skan      cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
17260169689Skan      list = chainon (list,
17261169689Skan		      build_tree_list (NULL_TREE,
17262169689Skan				       cp_parser_identifier (parser)));
17263169689Skan      sep = cp_lexer_peek_token (parser->lexer);
17264169689Skan    }
17265169689Skan
17266169689Skan  return list;
17267169689Skan}
17268169689Skan
17269169689Skan/* Parse an Objective-C alias declaration.
17270169689Skan
17271169689Skan   objc-alias-declaration:
17272169689Skan     @compatibility_alias identifier identifier ;
17273169689Skan
17274169689Skan   This function registers the alias mapping with the Objective-C front-end.
17275169689Skan   It returns nothing.  */
17276169689Skan
17277169689Skanstatic void
17278169689Skancp_parser_objc_alias_declaration (cp_parser* parser)
17279169689Skan{
17280169689Skan  tree alias, orig;
17281169689Skan
17282169689Skan  cp_lexer_consume_token (parser->lexer);  /* Eat '@compatibility_alias'.  */
17283169689Skan  alias = cp_parser_identifier (parser);
17284169689Skan  orig = cp_parser_identifier (parser);
17285169689Skan  objc_declare_alias (alias, orig);
17286169689Skan  cp_parser_consume_semicolon_at_end_of_statement (parser);
17287169689Skan}
17288169689Skan
17289169689Skan/* Parse an Objective-C class forward-declaration.
17290169689Skan
17291169689Skan   objc-class-declaration:
17292169689Skan     @class objc-identifier-list ;
17293169689Skan
17294169689Skan   The function registers the forward declarations with the Objective-C
17295169689Skan   front-end.  It returns nothing.  */
17296169689Skan
17297169689Skanstatic void
17298169689Skancp_parser_objc_class_declaration (cp_parser* parser)
17299169689Skan{
17300169689Skan  cp_lexer_consume_token (parser->lexer);  /* Eat '@class'.  */
17301169689Skan  objc_declare_class (cp_parser_objc_identifier_list (parser));
17302169689Skan  cp_parser_consume_semicolon_at_end_of_statement (parser);
17303169689Skan}
17304169689Skan
17305169689Skan/* Parse a list of Objective-C protocol references.
17306169689Skan
17307169689Skan   objc-protocol-refs-opt:
17308169689Skan     objc-protocol-refs [opt]
17309169689Skan
17310169689Skan   objc-protocol-refs:
17311169689Skan     < objc-identifier-list >
17312169689Skan
17313169689Skan   Returns a TREE_LIST of identifiers, if any.  */
17314169689Skan
17315169689Skanstatic tree
17316169689Skancp_parser_objc_protocol_refs_opt (cp_parser* parser)
17317169689Skan{
17318169689Skan  tree protorefs = NULL_TREE;
17319169689Skan
17320169689Skan  if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
17321169689Skan    {
17322169689Skan      cp_lexer_consume_token (parser->lexer);  /* Eat '<'.  */
17323169689Skan      protorefs = cp_parser_objc_identifier_list (parser);
17324169689Skan      cp_parser_require (parser, CPP_GREATER, "`>'");
17325169689Skan    }
17326169689Skan
17327169689Skan  return protorefs;
17328169689Skan}
17329169689Skan
17330169689Skan/* Parse a Objective-C visibility specification.  */
17331169689Skan
17332169689Skanstatic void
17333169689Skancp_parser_objc_visibility_spec (cp_parser* parser)
17334169689Skan{
17335169689Skan  cp_token *vis = cp_lexer_peek_token (parser->lexer);
17336169689Skan
17337169689Skan  switch (vis->keyword)
17338169689Skan    {
17339169689Skan    case RID_AT_PRIVATE:
17340169689Skan      objc_set_visibility (2);
17341169689Skan      break;
17342169689Skan    case RID_AT_PROTECTED:
17343169689Skan      objc_set_visibility (0);
17344169689Skan      break;
17345169689Skan    case RID_AT_PUBLIC:
17346169689Skan      objc_set_visibility (1);
17347169689Skan      break;
17348169689Skan    default:
17349169689Skan      return;
17350169689Skan    }
17351169689Skan
17352169689Skan  /* Eat '@private'/'@protected'/'@public'.  */
17353169689Skan  cp_lexer_consume_token (parser->lexer);
17354169689Skan}
17355169689Skan
17356169689Skan/* Parse an Objective-C method type.  */
17357169689Skan
17358169689Skanstatic void
17359169689Skancp_parser_objc_method_type (cp_parser* parser)
17360169689Skan{
17361169689Skan  objc_set_method_type
17362169689Skan   (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
17363169689Skan    ? PLUS_EXPR
17364169689Skan    : MINUS_EXPR);
17365169689Skan}
17366169689Skan
17367169689Skan/* Parse an Objective-C protocol qualifier.  */
17368169689Skan
17369169689Skanstatic tree
17370169689Skancp_parser_objc_protocol_qualifiers (cp_parser* parser)
17371169689Skan{
17372169689Skan  tree quals = NULL_TREE, node;
17373169689Skan  cp_token *token = cp_lexer_peek_token (parser->lexer);
17374169689Skan
17375169689Skan  node = token->u.value;
17376169689Skan
17377169689Skan  while (node && TREE_CODE (node) == IDENTIFIER_NODE
17378169689Skan	 && (node == ridpointers [(int) RID_IN]
17379169689Skan	     || node == ridpointers [(int) RID_OUT]
17380169689Skan	     || node == ridpointers [(int) RID_INOUT]
17381169689Skan	     || node == ridpointers [(int) RID_BYCOPY]
17382169689Skan	     || node == ridpointers [(int) RID_BYREF]
17383169689Skan	     || node == ridpointers [(int) RID_ONEWAY]))
17384169689Skan    {
17385169689Skan      quals = tree_cons (NULL_TREE, node, quals);
17386169689Skan      cp_lexer_consume_token (parser->lexer);
17387169689Skan      token = cp_lexer_peek_token (parser->lexer);
17388169689Skan      node = token->u.value;
17389169689Skan    }
17390169689Skan
17391169689Skan  return quals;
17392169689Skan}
17393169689Skan
17394169689Skan/* Parse an Objective-C typename.  */
17395169689Skan
17396169689Skanstatic tree
17397169689Skancp_parser_objc_typename (cp_parser* parser)
17398169689Skan{
17399169689Skan  tree typename = NULL_TREE;
17400169689Skan
17401169689Skan  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
17402169689Skan    {
17403169689Skan      tree proto_quals, cp_type = NULL_TREE;
17404169689Skan
17405169689Skan      cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
17406169689Skan      proto_quals = cp_parser_objc_protocol_qualifiers (parser);
17407169689Skan
17408169689Skan      /* An ObjC type name may consist of just protocol qualifiers, in which
17409169689Skan	 case the type shall default to 'id'.  */
17410169689Skan      if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
17411169689Skan	cp_type = cp_parser_type_id (parser);
17412169689Skan
17413169689Skan      cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17414169689Skan      typename = build_tree_list (proto_quals, cp_type);
17415169689Skan    }
17416169689Skan
17417169689Skan  return typename;
17418169689Skan}
17419169689Skan
17420169689Skan/* Check to see if TYPE refers to an Objective-C selector name.  */
17421169689Skan
17422169689Skanstatic bool
17423169689Skancp_parser_objc_selector_p (enum cpp_ttype type)
17424169689Skan{
17425169689Skan  return (type == CPP_NAME || type == CPP_KEYWORD
17426169689Skan	  || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
17427169689Skan	  || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
17428169689Skan	  || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
17429169689Skan	  || type == CPP_XOR || type == CPP_XOR_EQ);
17430169689Skan}
17431169689Skan
17432169689Skan/* Parse an Objective-C selector.  */
17433169689Skan
17434169689Skanstatic tree
17435169689Skancp_parser_objc_selector (cp_parser* parser)
17436169689Skan{
17437169689Skan  cp_token *token = cp_lexer_consume_token (parser->lexer);
17438169689Skan
17439169689Skan  if (!cp_parser_objc_selector_p (token->type))
17440169689Skan    {
17441169689Skan      error ("invalid Objective-C++ selector name");
17442169689Skan      return error_mark_node;
17443169689Skan    }
17444169689Skan
17445169689Skan  /* C++ operator names are allowed to appear in ObjC selectors.  */
17446169689Skan  switch (token->type)
17447169689Skan    {
17448169689Skan    case CPP_AND_AND: return get_identifier ("and");
17449169689Skan    case CPP_AND_EQ: return get_identifier ("and_eq");
17450169689Skan    case CPP_AND: return get_identifier ("bitand");
17451169689Skan    case CPP_OR: return get_identifier ("bitor");
17452169689Skan    case CPP_COMPL: return get_identifier ("compl");
17453169689Skan    case CPP_NOT: return get_identifier ("not");
17454169689Skan    case CPP_NOT_EQ: return get_identifier ("not_eq");
17455169689Skan    case CPP_OR_OR: return get_identifier ("or");
17456169689Skan    case CPP_OR_EQ: return get_identifier ("or_eq");
17457169689Skan    case CPP_XOR: return get_identifier ("xor");
17458169689Skan    case CPP_XOR_EQ: return get_identifier ("xor_eq");
17459169689Skan    default: return token->u.value;
17460169689Skan    }
17461169689Skan}
17462169689Skan
17463169689Skan/* Parse an Objective-C params list.  */
17464169689Skan
17465169689Skanstatic tree
17466169689Skancp_parser_objc_method_keyword_params (cp_parser* parser)
17467169689Skan{
17468169689Skan  tree params = NULL_TREE;
17469169689Skan  bool maybe_unary_selector_p = true;
17470169689Skan  cp_token *token = cp_lexer_peek_token (parser->lexer);
17471169689Skan
17472169689Skan  while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
17473169689Skan    {
17474169689Skan      tree selector = NULL_TREE, typename, identifier;
17475169689Skan
17476169689Skan      if (token->type != CPP_COLON)
17477169689Skan	selector = cp_parser_objc_selector (parser);
17478169689Skan
17479169689Skan      /* Detect if we have a unary selector.  */
17480169689Skan      if (maybe_unary_selector_p
17481169689Skan	  && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
17482169689Skan	return selector;
17483169689Skan
17484169689Skan      maybe_unary_selector_p = false;
17485169689Skan      cp_parser_require (parser, CPP_COLON, "`:'");
17486169689Skan      typename = cp_parser_objc_typename (parser);
17487169689Skan      identifier = cp_parser_identifier (parser);
17488169689Skan
17489169689Skan      params
17490169689Skan	= chainon (params,
17491169689Skan		   objc_build_keyword_decl (selector,
17492169689Skan					    typename,
17493169689Skan					    identifier));
17494169689Skan
17495169689Skan      token = cp_lexer_peek_token (parser->lexer);
17496169689Skan    }
17497169689Skan
17498169689Skan  return params;
17499169689Skan}
17500169689Skan
17501169689Skan/* Parse the non-keyword Objective-C params.  */
17502169689Skan
17503169689Skanstatic tree
17504169689Skancp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
17505169689Skan{
17506169689Skan  tree params = make_node (TREE_LIST);
17507169689Skan  cp_token *token = cp_lexer_peek_token (parser->lexer);
17508169689Skan  *ellipsisp = false;  /* Initially, assume no ellipsis.  */
17509169689Skan
17510169689Skan  while (token->type == CPP_COMMA)
17511169689Skan    {
17512169689Skan      cp_parameter_declarator *parmdecl;
17513169689Skan      tree parm;
17514169689Skan
17515169689Skan      cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
17516169689Skan      token = cp_lexer_peek_token (parser->lexer);
17517169689Skan
17518169689Skan      if (token->type == CPP_ELLIPSIS)
17519169689Skan	{
17520169689Skan	  cp_lexer_consume_token (parser->lexer);  /* Eat '...'.  */
17521169689Skan	  *ellipsisp = true;
17522169689Skan	  break;
17523169689Skan	}
17524169689Skan
17525169689Skan      parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
17526169689Skan      parm = grokdeclarator (parmdecl->declarator,
17527169689Skan			     &parmdecl->decl_specifiers,
17528169689Skan			     PARM, /*initialized=*/0,
17529169689Skan			     /*attrlist=*/NULL);
17530169689Skan
17531169689Skan      chainon (params, build_tree_list (NULL_TREE, parm));
17532169689Skan      token = cp_lexer_peek_token (parser->lexer);
17533169689Skan    }
17534169689Skan
17535169689Skan  return params;
17536169689Skan}
17537169689Skan
17538169689Skan/* Parse a linkage specification, a pragma, an extra semicolon or a block.  */
17539169689Skan
17540169689Skanstatic void
17541169689Skancp_parser_objc_interstitial_code (cp_parser* parser)
17542169689Skan{
17543169689Skan  cp_token *token = cp_lexer_peek_token (parser->lexer);
17544169689Skan
17545169689Skan  /* If the next token is `extern' and the following token is a string
17546169689Skan     literal, then we have a linkage specification.  */
17547169689Skan  if (token->keyword == RID_EXTERN
17548169689Skan      && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
17549169689Skan    cp_parser_linkage_specification (parser);
17550169689Skan  /* Handle #pragma, if any.  */
17551169689Skan  else if (token->type == CPP_PRAGMA)
17552169689Skan    cp_parser_pragma (parser, pragma_external);
17553169689Skan  /* Allow stray semicolons.  */
17554169689Skan  else if (token->type == CPP_SEMICOLON)
17555169689Skan    cp_lexer_consume_token (parser->lexer);
17556169689Skan  /* Finally, try to parse a block-declaration, or a function-definition.  */
17557169689Skan  else
17558169689Skan    cp_parser_block_declaration (parser, /*statement_p=*/false);
17559169689Skan}
17560169689Skan
17561169689Skan/* Parse a method signature.  */
17562169689Skan
17563169689Skanstatic tree
17564169689Skancp_parser_objc_method_signature (cp_parser* parser)
17565169689Skan{
17566169689Skan  tree rettype, kwdparms, optparms;
17567169689Skan  bool ellipsis = false;
17568169689Skan
17569169689Skan  cp_parser_objc_method_type (parser);
17570169689Skan  rettype = cp_parser_objc_typename (parser);
17571169689Skan  kwdparms = cp_parser_objc_method_keyword_params (parser);
17572169689Skan  optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
17573169689Skan
17574169689Skan  return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
17575169689Skan}
17576169689Skan
17577169689Skan/* Pars an Objective-C method prototype list.  */
17578169689Skan
17579169689Skanstatic void
17580169689Skancp_parser_objc_method_prototype_list (cp_parser* parser)
17581169689Skan{
17582169689Skan  cp_token *token = cp_lexer_peek_token (parser->lexer);
17583169689Skan
17584169689Skan  while (token->keyword != RID_AT_END)
17585169689Skan    {
17586169689Skan      if (token->type == CPP_PLUS || token->type == CPP_MINUS)
17587169689Skan	{
17588169689Skan	  objc_add_method_declaration
17589169689Skan	   (cp_parser_objc_method_signature (parser));
17590169689Skan	  cp_parser_consume_semicolon_at_end_of_statement (parser);
17591169689Skan	}
17592169689Skan      else
17593169689Skan	/* Allow for interspersed non-ObjC++ code.  */
17594169689Skan	cp_parser_objc_interstitial_code (parser);
17595169689Skan
17596169689Skan      token = cp_lexer_peek_token (parser->lexer);
17597169689Skan    }
17598169689Skan
17599169689Skan  cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
17600169689Skan  objc_finish_interface ();
17601169689Skan}
17602169689Skan
17603169689Skan/* Parse an Objective-C method definition list.  */
17604169689Skan
17605169689Skanstatic void
17606169689Skancp_parser_objc_method_definition_list (cp_parser* parser)
17607169689Skan{
17608169689Skan  cp_token *token = cp_lexer_peek_token (parser->lexer);
17609169689Skan
17610169689Skan  while (token->keyword != RID_AT_END)
17611169689Skan    {
17612169689Skan      tree meth;
17613169689Skan
17614169689Skan      if (token->type == CPP_PLUS || token->type == CPP_MINUS)
17615169689Skan	{
17616169689Skan	  push_deferring_access_checks (dk_deferred);
17617169689Skan	  objc_start_method_definition
17618169689Skan	   (cp_parser_objc_method_signature (parser));
17619169689Skan
17620169689Skan	  /* For historical reasons, we accept an optional semicolon.  */
17621169689Skan	  if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17622169689Skan	    cp_lexer_consume_token (parser->lexer);
17623169689Skan
17624169689Skan	  perform_deferred_access_checks ();
17625169689Skan	  stop_deferring_access_checks ();
17626169689Skan	  meth = cp_parser_function_definition_after_declarator (parser,
17627169689Skan								 false);
17628169689Skan	  pop_deferring_access_checks ();
17629169689Skan	  objc_finish_method_definition (meth);
17630169689Skan	}
17631169689Skan      else
17632169689Skan	/* Allow for interspersed non-ObjC++ code.  */
17633169689Skan	cp_parser_objc_interstitial_code (parser);
17634169689Skan
17635169689Skan      token = cp_lexer_peek_token (parser->lexer);
17636169689Skan    }
17637169689Skan
17638169689Skan  cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
17639169689Skan  objc_finish_implementation ();
17640169689Skan}
17641169689Skan
17642169689Skan/* Parse Objective-C ivars.  */
17643169689Skan
17644169689Skanstatic void
17645169689Skancp_parser_objc_class_ivars (cp_parser* parser)
17646169689Skan{
17647169689Skan  cp_token *token = cp_lexer_peek_token (parser->lexer);
17648169689Skan
17649169689Skan  if (token->type != CPP_OPEN_BRACE)
17650169689Skan    return;	/* No ivars specified.  */
17651169689Skan
17652169689Skan  cp_lexer_consume_token (parser->lexer);  /* Eat '{'.  */
17653169689Skan  token = cp_lexer_peek_token (parser->lexer);
17654169689Skan
17655169689Skan  while (token->type != CPP_CLOSE_BRACE)
17656169689Skan    {
17657169689Skan      cp_decl_specifier_seq declspecs;
17658169689Skan      int decl_class_or_enum_p;
17659169689Skan      tree prefix_attributes;
17660169689Skan
17661169689Skan      cp_parser_objc_visibility_spec (parser);
17662169689Skan
17663169689Skan      if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
17664169689Skan	break;
17665169689Skan
17666169689Skan      cp_parser_decl_specifier_seq (parser,
17667169689Skan				    CP_PARSER_FLAGS_OPTIONAL,
17668169689Skan				    &declspecs,
17669169689Skan				    &decl_class_or_enum_p);
17670169689Skan      prefix_attributes = declspecs.attributes;
17671169689Skan      declspecs.attributes = NULL_TREE;
17672169689Skan
17673169689Skan      /* Keep going until we hit the `;' at the end of the
17674169689Skan	 declaration.  */
17675169689Skan      while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17676169689Skan	{
17677169689Skan	  tree width = NULL_TREE, attributes, first_attribute, decl;
17678169689Skan	  cp_declarator *declarator = NULL;
17679169689Skan	  int ctor_dtor_or_conv_p;
17680169689Skan
17681169689Skan	  /* Check for a (possibly unnamed) bitfield declaration.  */
17682169689Skan	  token = cp_lexer_peek_token (parser->lexer);
17683169689Skan	  if (token->type == CPP_COLON)
17684169689Skan	    goto eat_colon;
17685169689Skan
17686169689Skan	  if (token->type == CPP_NAME
17687169689Skan	      && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
17688169689Skan		  == CPP_COLON))
17689169689Skan	    {
17690169689Skan	      /* Get the name of the bitfield.  */
17691169689Skan	      declarator = make_id_declarator (NULL_TREE,
17692169689Skan					       cp_parser_identifier (parser),
17693169689Skan					       sfk_none);
17694169689Skan
17695169689Skan	     eat_colon:
17696169689Skan	      cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
17697169689Skan	      /* Get the width of the bitfield.  */
17698169689Skan	      width
17699169689Skan		= cp_parser_constant_expression (parser,
17700169689Skan						 /*allow_non_constant=*/false,
17701169689Skan						 NULL);
17702169689Skan	    }
17703169689Skan	  else
17704169689Skan	    {
17705169689Skan	      /* Parse the declarator.  */
17706169689Skan	      declarator
17707169689Skan		= cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
17708169689Skan					&ctor_dtor_or_conv_p,
17709169689Skan					/*parenthesized_p=*/NULL,
17710169689Skan					/*member_p=*/false);
17711169689Skan	    }
17712169689Skan
17713169689Skan	  /* Look for attributes that apply to the ivar.  */
17714169689Skan	  attributes = cp_parser_attributes_opt (parser);
17715169689Skan	  /* Remember which attributes are prefix attributes and
17716169689Skan	     which are not.  */
17717169689Skan	  first_attribute = attributes;
17718169689Skan	  /* Combine the attributes.  */
17719169689Skan	  attributes = chainon (prefix_attributes, attributes);
17720169689Skan
17721169689Skan	  if (width)
17722169689Skan	    {
17723169689Skan	      /* Create the bitfield declaration.  */
17724169689Skan	      decl = grokbitfield (declarator, &declspecs, width);
17725169689Skan	      cplus_decl_attributes (&decl, attributes, /*flags=*/0);
17726169689Skan	    }
17727169689Skan	  else
17728169689Skan	    decl = grokfield (declarator, &declspecs,
17729169689Skan			      NULL_TREE, /*init_const_expr_p=*/false,
17730169689Skan			      NULL_TREE, attributes);
17731169689Skan
17732169689Skan	  /* Add the instance variable.  */
17733169689Skan	  objc_add_instance_variable (decl);
17734169689Skan
17735169689Skan	  /* Reset PREFIX_ATTRIBUTES.  */
17736169689Skan	  while (attributes && TREE_CHAIN (attributes) != first_attribute)
17737169689Skan	    attributes = TREE_CHAIN (attributes);
17738169689Skan	  if (attributes)
17739169689Skan	    TREE_CHAIN (attributes) = NULL_TREE;
17740169689Skan
17741169689Skan	  token = cp_lexer_peek_token (parser->lexer);
17742169689Skan
17743169689Skan	  if (token->type == CPP_COMMA)
17744169689Skan	    {
17745169689Skan	      cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
17746169689Skan	      continue;
17747169689Skan	    }
17748169689Skan	  break;
17749169689Skan	}
17750169689Skan
17751169689Skan      cp_parser_consume_semicolon_at_end_of_statement (parser);
17752169689Skan      token = cp_lexer_peek_token (parser->lexer);
17753169689Skan    }
17754169689Skan
17755169689Skan  cp_lexer_consume_token (parser->lexer);  /* Eat '}'.  */
17756169689Skan  /* For historical reasons, we accept an optional semicolon.  */
17757169689Skan  if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17758169689Skan    cp_lexer_consume_token (parser->lexer);
17759169689Skan}
17760169689Skan
17761169689Skan/* Parse an Objective-C protocol declaration.  */
17762169689Skan
17763169689Skanstatic void
17764169689Skancp_parser_objc_protocol_declaration (cp_parser* parser)
17765169689Skan{
17766169689Skan  tree proto, protorefs;
17767169689Skan  cp_token *tok;
17768169689Skan
17769169689Skan  cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
17770169689Skan  if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
17771169689Skan    {
17772169689Skan      error ("identifier expected after %<@protocol%>");
17773169689Skan      goto finish;
17774169689Skan    }
17775169689Skan
17776169689Skan  /* See if we have a forward declaration or a definition.  */
17777169689Skan  tok = cp_lexer_peek_nth_token (parser->lexer, 2);
17778169689Skan
17779169689Skan  /* Try a forward declaration first.  */
17780169689Skan  if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
17781169689Skan    {
17782169689Skan      objc_declare_protocols (cp_parser_objc_identifier_list (parser));
17783169689Skan     finish:
17784169689Skan      cp_parser_consume_semicolon_at_end_of_statement (parser);
17785169689Skan    }
17786169689Skan
17787169689Skan  /* Ok, we got a full-fledged definition (or at least should).  */
17788169689Skan  else
17789169689Skan    {
17790169689Skan      proto = cp_parser_identifier (parser);
17791169689Skan      protorefs = cp_parser_objc_protocol_refs_opt (parser);
17792169689Skan      objc_start_protocol (proto, protorefs);
17793169689Skan      cp_parser_objc_method_prototype_list (parser);
17794169689Skan    }
17795169689Skan}
17796169689Skan
17797169689Skan/* Parse an Objective-C superclass or category.  */
17798169689Skan
17799169689Skanstatic void
17800169689Skancp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
17801169689Skan							  tree *categ)
17802169689Skan{
17803169689Skan  cp_token *next = cp_lexer_peek_token (parser->lexer);
17804169689Skan
17805169689Skan  *super = *categ = NULL_TREE;
17806169689Skan  if (next->type == CPP_COLON)
17807169689Skan    {
17808169689Skan      cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
17809169689Skan      *super = cp_parser_identifier (parser);
17810169689Skan    }
17811169689Skan  else if (next->type == CPP_OPEN_PAREN)
17812169689Skan    {
17813169689Skan      cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
17814169689Skan      *categ = cp_parser_identifier (parser);
17815169689Skan      cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17816169689Skan    }
17817169689Skan}
17818169689Skan
17819169689Skan/* Parse an Objective-C class interface.  */
17820169689Skan
17821169689Skanstatic void
17822169689Skancp_parser_objc_class_interface (cp_parser* parser)
17823169689Skan{
17824169689Skan  tree name, super, categ, protos;
17825169689Skan
17826169689Skan  cp_lexer_consume_token (parser->lexer);  /* Eat '@interface'.  */
17827169689Skan  name = cp_parser_identifier (parser);
17828169689Skan  cp_parser_objc_superclass_or_category (parser, &super, &categ);
17829169689Skan  protos = cp_parser_objc_protocol_refs_opt (parser);
17830169689Skan
17831169689Skan  /* We have either a class or a category on our hands.  */
17832169689Skan  if (categ)
17833169689Skan    objc_start_category_interface (name, categ, protos);
17834169689Skan  else
17835169689Skan    {
17836169689Skan      objc_start_class_interface (name, super, protos);
17837169689Skan      /* Handle instance variable declarations, if any.  */
17838169689Skan      cp_parser_objc_class_ivars (parser);
17839169689Skan      objc_continue_interface ();
17840169689Skan    }
17841169689Skan
17842169689Skan  cp_parser_objc_method_prototype_list (parser);
17843169689Skan}
17844169689Skan
17845169689Skan/* Parse an Objective-C class implementation.  */
17846169689Skan
17847169689Skanstatic void
17848169689Skancp_parser_objc_class_implementation (cp_parser* parser)
17849169689Skan{
17850169689Skan  tree name, super, categ;
17851169689Skan
17852169689Skan  cp_lexer_consume_token (parser->lexer);  /* Eat '@implementation'.  */
17853169689Skan  name = cp_parser_identifier (parser);
17854169689Skan  cp_parser_objc_superclass_or_category (parser, &super, &categ);
17855169689Skan
17856169689Skan  /* We have either a class or a category on our hands.  */
17857169689Skan  if (categ)
17858169689Skan    objc_start_category_implementation (name, categ);
17859169689Skan  else
17860169689Skan    {
17861169689Skan      objc_start_class_implementation (name, super);
17862169689Skan      /* Handle instance variable declarations, if any.  */
17863169689Skan      cp_parser_objc_class_ivars (parser);
17864169689Skan      objc_continue_implementation ();
17865169689Skan    }
17866169689Skan
17867169689Skan  cp_parser_objc_method_definition_list (parser);
17868169689Skan}
17869169689Skan
17870169689Skan/* Consume the @end token and finish off the implementation.  */
17871169689Skan
17872169689Skanstatic void
17873169689Skancp_parser_objc_end_implementation (cp_parser* parser)
17874169689Skan{
17875169689Skan  cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
17876169689Skan  objc_finish_implementation ();
17877169689Skan}
17878169689Skan
17879169689Skan/* Parse an Objective-C declaration.  */
17880169689Skan
17881169689Skanstatic void
17882169689Skancp_parser_objc_declaration (cp_parser* parser)
17883169689Skan{
17884169689Skan  /* Try to figure out what kind of declaration is present.  */
17885169689Skan  cp_token *kwd = cp_lexer_peek_token (parser->lexer);
17886169689Skan
17887169689Skan  switch (kwd->keyword)
17888169689Skan    {
17889169689Skan    case RID_AT_ALIAS:
17890169689Skan      cp_parser_objc_alias_declaration (parser);
17891169689Skan      break;
17892169689Skan    case RID_AT_CLASS:
17893169689Skan      cp_parser_objc_class_declaration (parser);
17894169689Skan      break;
17895169689Skan    case RID_AT_PROTOCOL:
17896169689Skan      cp_parser_objc_protocol_declaration (parser);
17897169689Skan      break;
17898169689Skan    case RID_AT_INTERFACE:
17899169689Skan      cp_parser_objc_class_interface (parser);
17900169689Skan      break;
17901169689Skan    case RID_AT_IMPLEMENTATION:
17902169689Skan      cp_parser_objc_class_implementation (parser);
17903169689Skan      break;
17904169689Skan    case RID_AT_END:
17905169689Skan      cp_parser_objc_end_implementation (parser);
17906169689Skan      break;
17907169689Skan    default:
17908169689Skan      error ("misplaced %<@%D%> Objective-C++ construct", kwd->u.value);
17909169689Skan      cp_parser_skip_to_end_of_block_or_statement (parser);
17910169689Skan    }
17911169689Skan}
17912169689Skan
17913169689Skan/* Parse an Objective-C try-catch-finally statement.
17914169689Skan
17915169689Skan   objc-try-catch-finally-stmt:
17916169689Skan     @try compound-statement objc-catch-clause-seq [opt]
17917169689Skan       objc-finally-clause [opt]
17918169689Skan
17919169689Skan   objc-catch-clause-seq:
17920169689Skan     objc-catch-clause objc-catch-clause-seq [opt]
17921169689Skan
17922169689Skan   objc-catch-clause:
17923169689Skan     @catch ( exception-declaration ) compound-statement
17924169689Skan
17925169689Skan   objc-finally-clause
17926169689Skan     @finally compound-statement
17927169689Skan
17928169689Skan   Returns NULL_TREE.  */
17929169689Skan
17930169689Skanstatic tree
17931169689Skancp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
17932169689Skan  location_t location;
17933169689Skan  tree stmt;
17934169689Skan
17935169689Skan  cp_parser_require_keyword (parser, RID_AT_TRY, "`@try'");
17936169689Skan  location = cp_lexer_peek_token (parser->lexer)->location;
17937169689Skan  /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
17938169689Skan     node, lest it get absorbed into the surrounding block.  */
17939169689Skan  stmt = push_stmt_list ();
17940169689Skan  cp_parser_compound_statement (parser, NULL, false);
17941169689Skan  objc_begin_try_stmt (location, pop_stmt_list (stmt));
17942169689Skan
17943169689Skan  while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
17944169689Skan    {
17945169689Skan      cp_parameter_declarator *parmdecl;
17946169689Skan      tree parm;
17947169689Skan
17948169689Skan      cp_lexer_consume_token (parser->lexer);
17949169689Skan      cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17950169689Skan      parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
17951169689Skan      parm = grokdeclarator (parmdecl->declarator,
17952169689Skan			     &parmdecl->decl_specifiers,
17953169689Skan			     PARM, /*initialized=*/0,
17954169689Skan			     /*attrlist=*/NULL);
17955169689Skan      cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17956169689Skan      objc_begin_catch_clause (parm);
17957169689Skan      cp_parser_compound_statement (parser, NULL, false);
17958169689Skan      objc_finish_catch_clause ();
17959169689Skan    }
17960169689Skan
17961169689Skan  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
17962169689Skan    {
17963169689Skan      cp_lexer_consume_token (parser->lexer);
17964169689Skan      location = cp_lexer_peek_token (parser->lexer)->location;
17965169689Skan      /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
17966169689Skan	 node, lest it get absorbed into the surrounding block.  */
17967169689Skan      stmt = push_stmt_list ();
17968169689Skan      cp_parser_compound_statement (parser, NULL, false);
17969169689Skan      objc_build_finally_clause (location, pop_stmt_list (stmt));
17970169689Skan    }
17971169689Skan
17972169689Skan  return objc_finish_try_stmt ();
17973169689Skan}
17974169689Skan
17975169689Skan/* Parse an Objective-C synchronized statement.
17976169689Skan
17977169689Skan   objc-synchronized-stmt:
17978169689Skan     @synchronized ( expression ) compound-statement
17979169689Skan
17980169689Skan   Returns NULL_TREE.  */
17981169689Skan
17982169689Skanstatic tree
17983169689Skancp_parser_objc_synchronized_statement (cp_parser *parser) {
17984169689Skan  location_t location;
17985169689Skan  tree lock, stmt;
17986169689Skan
17987169689Skan  cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "`@synchronized'");
17988169689Skan
17989169689Skan  location = cp_lexer_peek_token (parser->lexer)->location;
17990169689Skan  cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17991169689Skan  lock = cp_parser_expression (parser, false);
17992169689Skan  cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17993169689Skan
17994169689Skan  /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
17995169689Skan     node, lest it get absorbed into the surrounding block.  */
17996169689Skan  stmt = push_stmt_list ();
17997169689Skan  cp_parser_compound_statement (parser, NULL, false);
17998169689Skan
17999169689Skan  return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
18000169689Skan}
18001169689Skan
18002169689Skan/* Parse an Objective-C throw statement.
18003169689Skan
18004169689Skan   objc-throw-stmt:
18005169689Skan     @throw assignment-expression [opt] ;
18006169689Skan
18007169689Skan   Returns a constructed '@throw' statement.  */
18008169689Skan
18009169689Skanstatic tree
18010169689Skancp_parser_objc_throw_statement (cp_parser *parser) {
18011169689Skan  tree expr = NULL_TREE;
18012169689Skan
18013169689Skan  cp_parser_require_keyword (parser, RID_AT_THROW, "`@throw'");
18014169689Skan
18015169689Skan  if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18016169689Skan    expr = cp_parser_assignment_expression (parser, false);
18017169689Skan
18018169689Skan  cp_parser_consume_semicolon_at_end_of_statement (parser);
18019169689Skan
18020169689Skan  return objc_build_throw_stmt (expr);
18021169689Skan}
18022169689Skan
18023169689Skan/* Parse an Objective-C statement.  */
18024169689Skan
18025169689Skanstatic tree
18026169689Skancp_parser_objc_statement (cp_parser * parser) {
18027169689Skan  /* Try to figure out what kind of declaration is present.  */
18028169689Skan  cp_token *kwd = cp_lexer_peek_token (parser->lexer);
18029169689Skan
18030169689Skan  switch (kwd->keyword)
18031169689Skan    {
18032169689Skan    case RID_AT_TRY:
18033169689Skan      return cp_parser_objc_try_catch_finally_statement (parser);
18034169689Skan    case RID_AT_SYNCHRONIZED:
18035169689Skan      return cp_parser_objc_synchronized_statement (parser);
18036169689Skan    case RID_AT_THROW:
18037169689Skan      return cp_parser_objc_throw_statement (parser);
18038169689Skan    default:
18039169689Skan      error ("misplaced %<@%D%> Objective-C++ construct", kwd->u.value);
18040169689Skan      cp_parser_skip_to_end_of_block_or_statement (parser);
18041169689Skan    }
18042169689Skan
18043169689Skan  return error_mark_node;
18044169689Skan}
18045132718Skan
18046169689Skan/* OpenMP 2.5 parsing routines.  */
18047132718Skan
18048169689Skan/* All OpenMP clauses.  OpenMP 2.5.  */
18049169689Skantypedef enum pragma_omp_clause {
18050169689Skan  PRAGMA_OMP_CLAUSE_NONE = 0,
18051169689Skan
18052169689Skan  PRAGMA_OMP_CLAUSE_COPYIN,
18053169689Skan  PRAGMA_OMP_CLAUSE_COPYPRIVATE,
18054169689Skan  PRAGMA_OMP_CLAUSE_DEFAULT,
18055169689Skan  PRAGMA_OMP_CLAUSE_FIRSTPRIVATE,
18056169689Skan  PRAGMA_OMP_CLAUSE_IF,
18057169689Skan  PRAGMA_OMP_CLAUSE_LASTPRIVATE,
18058169689Skan  PRAGMA_OMP_CLAUSE_NOWAIT,
18059169689Skan  PRAGMA_OMP_CLAUSE_NUM_THREADS,
18060169689Skan  PRAGMA_OMP_CLAUSE_ORDERED,
18061169689Skan  PRAGMA_OMP_CLAUSE_PRIVATE,
18062169689Skan  PRAGMA_OMP_CLAUSE_REDUCTION,
18063169689Skan  PRAGMA_OMP_CLAUSE_SCHEDULE,
18064169689Skan  PRAGMA_OMP_CLAUSE_SHARED
18065169689Skan} pragma_omp_clause;
18066169689Skan
18067169689Skan/* Returns name of the next clause.
18068169689Skan   If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
18069169689Skan   the token is not consumed.  Otherwise appropriate pragma_omp_clause is
18070169689Skan   returned and the token is consumed.  */
18071169689Skan
18072169689Skanstatic pragma_omp_clause
18073169689Skancp_parser_omp_clause_name (cp_parser *parser)
18074169689Skan{
18075169689Skan  pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
18076169689Skan
18077169689Skan  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
18078169689Skan    result = PRAGMA_OMP_CLAUSE_IF;
18079169689Skan  else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
18080169689Skan    result = PRAGMA_OMP_CLAUSE_DEFAULT;
18081169689Skan  else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
18082169689Skan    result = PRAGMA_OMP_CLAUSE_PRIVATE;
18083169689Skan  else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18084169689Skan    {
18085169689Skan      tree id = cp_lexer_peek_token (parser->lexer)->u.value;
18086169689Skan      const char *p = IDENTIFIER_POINTER (id);
18087169689Skan
18088169689Skan      switch (p[0])
18089169689Skan	{
18090169689Skan	case 'c':
18091169689Skan	  if (!strcmp ("copyin", p))
18092169689Skan	    result = PRAGMA_OMP_CLAUSE_COPYIN;
18093169689Skan	  else if (!strcmp ("copyprivate", p))
18094169689Skan	    result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
18095169689Skan	  break;
18096169689Skan	case 'f':
18097169689Skan	  if (!strcmp ("firstprivate", p))
18098169689Skan	    result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
18099169689Skan	  break;
18100169689Skan	case 'l':
18101169689Skan	  if (!strcmp ("lastprivate", p))
18102169689Skan	    result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
18103169689Skan	  break;
18104169689Skan	case 'n':
18105169689Skan	  if (!strcmp ("nowait", p))
18106169689Skan	    result = PRAGMA_OMP_CLAUSE_NOWAIT;
18107169689Skan	  else if (!strcmp ("num_threads", p))
18108169689Skan	    result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
18109169689Skan	  break;
18110169689Skan	case 'o':
18111169689Skan	  if (!strcmp ("ordered", p))
18112169689Skan	    result = PRAGMA_OMP_CLAUSE_ORDERED;
18113169689Skan	  break;
18114169689Skan	case 'r':
18115169689Skan	  if (!strcmp ("reduction", p))
18116169689Skan	    result = PRAGMA_OMP_CLAUSE_REDUCTION;
18117169689Skan	  break;
18118169689Skan	case 's':
18119169689Skan	  if (!strcmp ("schedule", p))
18120169689Skan	    result = PRAGMA_OMP_CLAUSE_SCHEDULE;
18121169689Skan	  else if (!strcmp ("shared", p))
18122169689Skan	    result = PRAGMA_OMP_CLAUSE_SHARED;
18123169689Skan	  break;
18124169689Skan	}
18125169689Skan    }
18126169689Skan
18127169689Skan  if (result != PRAGMA_OMP_CLAUSE_NONE)
18128169689Skan    cp_lexer_consume_token (parser->lexer);
18129169689Skan
18130169689Skan  return result;
18131169689Skan}
18132169689Skan
18133169689Skan/* Validate that a clause of the given type does not already exist.  */
18134169689Skan
18135169689Skanstatic void
18136169689Skancheck_no_duplicate_clause (tree clauses, enum tree_code code, const char *name)
18137169689Skan{
18138169689Skan  tree c;
18139169689Skan
18140169689Skan  for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
18141169689Skan    if (OMP_CLAUSE_CODE (c) == code)
18142169689Skan      {
18143169689Skan	error ("too many %qs clauses", name);
18144169689Skan	break;
18145169689Skan      }
18146169689Skan}
18147169689Skan
18148169689Skan/* OpenMP 2.5:
18149169689Skan   variable-list:
18150169689Skan     identifier
18151169689Skan     variable-list , identifier
18152169689Skan
18153169689Skan   In addition, we match a closing parenthesis.  An opening parenthesis
18154169689Skan   will have been consumed by the caller.
18155169689Skan
18156169689Skan   If KIND is nonzero, create the appropriate node and install the decl
18157169689Skan   in OMP_CLAUSE_DECL and add the node to the head of the list.
18158169689Skan
18159169689Skan   If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
18160169689Skan   return the list created.  */
18161169689Skan
18162169689Skanstatic tree
18163169689Skancp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
18164169689Skan				tree list)
18165169689Skan{
18166169689Skan  while (1)
18167169689Skan    {
18168169689Skan      tree name, decl;
18169169689Skan
18170169689Skan      name = cp_parser_id_expression (parser, /*template_p=*/false,
18171169689Skan				      /*check_dependency_p=*/true,
18172169689Skan				      /*template_p=*/NULL,
18173169689Skan				      /*declarator_p=*/false,
18174169689Skan				      /*optional_p=*/false);
18175169689Skan      if (name == error_mark_node)
18176169689Skan	goto skip_comma;
18177169689Skan
18178169689Skan      decl = cp_parser_lookup_name_simple (parser, name);
18179169689Skan      if (decl == error_mark_node)
18180169689Skan	cp_parser_name_lookup_error (parser, name, decl, NULL);
18181169689Skan      else if (kind != 0)
18182169689Skan	{
18183169689Skan	  tree u = build_omp_clause (kind);
18184169689Skan	  OMP_CLAUSE_DECL (u) = decl;
18185169689Skan	  OMP_CLAUSE_CHAIN (u) = list;
18186169689Skan	  list = u;
18187169689Skan	}
18188169689Skan      else
18189169689Skan	list = tree_cons (decl, NULL_TREE, list);
18190169689Skan
18191169689Skan    get_comma:
18192169689Skan      if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18193169689Skan	break;
18194169689Skan      cp_lexer_consume_token (parser->lexer);
18195169689Skan    }
18196169689Skan
18197169689Skan  if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18198169689Skan    {
18199169689Skan      int ending;
18200169689Skan
18201169689Skan      /* Try to resync to an unnested comma.  Copied from
18202169689Skan	 cp_parser_parenthesized_expression_list.  */
18203169689Skan    skip_comma:
18204169689Skan      ending = cp_parser_skip_to_closing_parenthesis (parser,
18205169689Skan						      /*recovering=*/true,
18206169689Skan						      /*or_comma=*/true,
18207169689Skan						      /*consume_paren=*/true);
18208169689Skan      if (ending < 0)
18209169689Skan	goto get_comma;
18210169689Skan    }
18211169689Skan
18212169689Skan  return list;
18213169689Skan}
18214169689Skan
18215169689Skan/* Similarly, but expect leading and trailing parenthesis.  This is a very
18216169689Skan   common case for omp clauses.  */
18217169689Skan
18218169689Skanstatic tree
18219169689Skancp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
18220169689Skan{
18221169689Skan  if (cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18222169689Skan    return cp_parser_omp_var_list_no_open (parser, kind, list);
18223169689Skan  return list;
18224169689Skan}
18225169689Skan
18226169689Skan/* OpenMP 2.5:
18227169689Skan   default ( shared | none ) */
18228169689Skan
18229169689Skanstatic tree
18230169689Skancp_parser_omp_clause_default (cp_parser *parser, tree list)
18231169689Skan{
18232169689Skan  enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
18233169689Skan  tree c;
18234169689Skan
18235169689Skan  if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18236169689Skan    return list;
18237169689Skan  if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18238169689Skan    {
18239169689Skan      tree id = cp_lexer_peek_token (parser->lexer)->u.value;
18240169689Skan      const char *p = IDENTIFIER_POINTER (id);
18241169689Skan
18242169689Skan      switch (p[0])
18243169689Skan	{
18244169689Skan	case 'n':
18245169689Skan	  if (strcmp ("none", p) != 0)
18246169689Skan	    goto invalid_kind;
18247169689Skan	  kind = OMP_CLAUSE_DEFAULT_NONE;
18248169689Skan	  break;
18249169689Skan
18250169689Skan	case 's':
18251169689Skan	  if (strcmp ("shared", p) != 0)
18252169689Skan	    goto invalid_kind;
18253169689Skan	  kind = OMP_CLAUSE_DEFAULT_SHARED;
18254169689Skan	  break;
18255169689Skan
18256169689Skan	default:
18257169689Skan	  goto invalid_kind;
18258169689Skan	}
18259169689Skan
18260169689Skan      cp_lexer_consume_token (parser->lexer);
18261169689Skan    }
18262169689Skan  else
18263169689Skan    {
18264169689Skan    invalid_kind:
18265169689Skan      cp_parser_error (parser, "expected %<none%> or %<shared%>");
18266169689Skan    }
18267169689Skan
18268169689Skan  if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18269169689Skan    cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18270169689Skan					   /*or_comma=*/false,
18271169689Skan					   /*consume_paren=*/true);
18272169689Skan
18273169689Skan  if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
18274169689Skan    return list;
18275169689Skan
18276169689Skan  check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
18277169689Skan  c = build_omp_clause (OMP_CLAUSE_DEFAULT);
18278169689Skan  OMP_CLAUSE_CHAIN (c) = list;
18279169689Skan  OMP_CLAUSE_DEFAULT_KIND (c) = kind;
18280169689Skan
18281169689Skan  return c;
18282169689Skan}
18283169689Skan
18284169689Skan/* OpenMP 2.5:
18285169689Skan   if ( expression ) */
18286169689Skan
18287169689Skanstatic tree
18288169689Skancp_parser_omp_clause_if (cp_parser *parser, tree list)
18289169689Skan{
18290169689Skan  tree t, c;
18291169689Skan
18292169689Skan  if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18293169689Skan    return list;
18294169689Skan
18295169689Skan  t = cp_parser_condition (parser);
18296169689Skan
18297169689Skan  if (t == error_mark_node
18298169689Skan      || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18299169689Skan    cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18300169689Skan					   /*or_comma=*/false,
18301169689Skan					   /*consume_paren=*/true);
18302169689Skan
18303169689Skan  check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if");
18304169689Skan
18305169689Skan  c = build_omp_clause (OMP_CLAUSE_IF);
18306169689Skan  OMP_CLAUSE_IF_EXPR (c) = t;
18307169689Skan  OMP_CLAUSE_CHAIN (c) = list;
18308169689Skan
18309169689Skan  return c;
18310169689Skan}
18311169689Skan
18312169689Skan/* OpenMP 2.5:
18313169689Skan   nowait */
18314169689Skan
18315169689Skanstatic tree
18316169689Skancp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED, tree list)
18317169689Skan{
18318169689Skan  tree c;
18319169689Skan
18320169689Skan  check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
18321169689Skan
18322169689Skan  c = build_omp_clause (OMP_CLAUSE_NOWAIT);
18323169689Skan  OMP_CLAUSE_CHAIN (c) = list;
18324169689Skan  return c;
18325169689Skan}
18326169689Skan
18327169689Skan/* OpenMP 2.5:
18328169689Skan   num_threads ( expression ) */
18329169689Skan
18330169689Skanstatic tree
18331169689Skancp_parser_omp_clause_num_threads (cp_parser *parser, tree list)
18332169689Skan{
18333169689Skan  tree t, c;
18334169689Skan
18335169689Skan  if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18336169689Skan    return list;
18337169689Skan
18338169689Skan  t = cp_parser_expression (parser, false);
18339169689Skan
18340169689Skan  if (t == error_mark_node
18341169689Skan      || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18342169689Skan    cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18343169689Skan					   /*or_comma=*/false,
18344169689Skan					   /*consume_paren=*/true);
18345169689Skan
18346169689Skan  check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
18347169689Skan
18348169689Skan  c = build_omp_clause (OMP_CLAUSE_NUM_THREADS);
18349169689Skan  OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
18350169689Skan  OMP_CLAUSE_CHAIN (c) = list;
18351169689Skan
18352169689Skan  return c;
18353169689Skan}
18354169689Skan
18355169689Skan/* OpenMP 2.5:
18356169689Skan   ordered */
18357169689Skan
18358169689Skanstatic tree
18359169689Skancp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED, tree list)
18360169689Skan{
18361169689Skan  tree c;
18362169689Skan
18363169689Skan  check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
18364169689Skan
18365169689Skan  c = build_omp_clause (OMP_CLAUSE_ORDERED);
18366169689Skan  OMP_CLAUSE_CHAIN (c) = list;
18367169689Skan  return c;
18368169689Skan}
18369169689Skan
18370169689Skan/* OpenMP 2.5:
18371169689Skan   reduction ( reduction-operator : variable-list )
18372169689Skan
18373169689Skan   reduction-operator:
18374169689Skan     One of: + * - & ^ | && || */
18375169689Skan
18376169689Skanstatic tree
18377169689Skancp_parser_omp_clause_reduction (cp_parser *parser, tree list)
18378169689Skan{
18379169689Skan  enum tree_code code;
18380169689Skan  tree nlist, c;
18381169689Skan
18382169689Skan  if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18383169689Skan    return list;
18384169689Skan
18385169689Skan  switch (cp_lexer_peek_token (parser->lexer)->type)
18386169689Skan    {
18387169689Skan    case CPP_PLUS:
18388169689Skan      code = PLUS_EXPR;
18389169689Skan      break;
18390169689Skan    case CPP_MULT:
18391169689Skan      code = MULT_EXPR;
18392169689Skan      break;
18393169689Skan    case CPP_MINUS:
18394169689Skan      code = MINUS_EXPR;
18395169689Skan      break;
18396169689Skan    case CPP_AND:
18397169689Skan      code = BIT_AND_EXPR;
18398169689Skan      break;
18399169689Skan    case CPP_XOR:
18400169689Skan      code = BIT_XOR_EXPR;
18401169689Skan      break;
18402169689Skan    case CPP_OR:
18403169689Skan      code = BIT_IOR_EXPR;
18404169689Skan      break;
18405169689Skan    case CPP_AND_AND:
18406169689Skan      code = TRUTH_ANDIF_EXPR;
18407169689Skan      break;
18408169689Skan    case CPP_OR_OR:
18409169689Skan      code = TRUTH_ORIF_EXPR;
18410169689Skan      break;
18411169689Skan    default:
18412169689Skan      cp_parser_error (parser, "`+', `*', `-', `&', `^', `|', `&&', or `||'");
18413169689Skan    resync_fail:
18414169689Skan      cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18415169689Skan					     /*or_comma=*/false,
18416169689Skan					     /*consume_paren=*/true);
18417169689Skan      return list;
18418169689Skan    }
18419169689Skan  cp_lexer_consume_token (parser->lexer);
18420169689Skan
18421169689Skan  if (!cp_parser_require (parser, CPP_COLON, "`:'"))
18422169689Skan    goto resync_fail;
18423169689Skan
18424169689Skan  nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
18425169689Skan  for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
18426169689Skan    OMP_CLAUSE_REDUCTION_CODE (c) = code;
18427169689Skan
18428169689Skan  return nlist;
18429169689Skan}
18430169689Skan
18431169689Skan/* OpenMP 2.5:
18432169689Skan   schedule ( schedule-kind )
18433169689Skan   schedule ( schedule-kind , expression )
18434169689Skan
18435169689Skan   schedule-kind:
18436169689Skan     static | dynamic | guided | runtime  */
18437169689Skan
18438169689Skanstatic tree
18439169689Skancp_parser_omp_clause_schedule (cp_parser *parser, tree list)
18440169689Skan{
18441169689Skan  tree c, t;
18442169689Skan
18443169689Skan  if (!cp_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
18444169689Skan    return list;
18445169689Skan
18446169689Skan  c = build_omp_clause (OMP_CLAUSE_SCHEDULE);
18447169689Skan
18448169689Skan  if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18449169689Skan    {
18450169689Skan      tree id = cp_lexer_peek_token (parser->lexer)->u.value;
18451169689Skan      const char *p = IDENTIFIER_POINTER (id);
18452169689Skan
18453169689Skan      switch (p[0])
18454169689Skan	{
18455169689Skan	case 'd':
18456169689Skan	  if (strcmp ("dynamic", p) != 0)
18457169689Skan	    goto invalid_kind;
18458169689Skan	  OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
18459169689Skan	  break;
18460169689Skan
18461169689Skan	case 'g':
18462169689Skan	  if (strcmp ("guided", p) != 0)
18463169689Skan	    goto invalid_kind;
18464169689Skan	  OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
18465169689Skan	  break;
18466169689Skan
18467169689Skan	case 'r':
18468169689Skan	  if (strcmp ("runtime", p) != 0)
18469169689Skan	    goto invalid_kind;
18470169689Skan	  OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
18471169689Skan	  break;
18472169689Skan
18473169689Skan	default:
18474169689Skan	  goto invalid_kind;
18475169689Skan	}
18476169689Skan    }
18477169689Skan  else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
18478169689Skan    OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
18479169689Skan  else
18480169689Skan    goto invalid_kind;
18481169689Skan  cp_lexer_consume_token (parser->lexer);
18482169689Skan
18483169689Skan  if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18484169689Skan    {
18485169689Skan      cp_lexer_consume_token (parser->lexer);
18486169689Skan
18487169689Skan      t = cp_parser_assignment_expression (parser, false);
18488169689Skan
18489169689Skan      if (t == error_mark_node)
18490169689Skan	goto resync_fail;
18491169689Skan      else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
18492169689Skan	error ("schedule %<runtime%> does not take "
18493169689Skan	       "a %<chunk_size%> parameter");
18494169689Skan      else
18495169689Skan	OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
18496169689Skan
18497169689Skan      if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18498169689Skan	goto resync_fail;
18499169689Skan    }
18500169689Skan  else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`,' or `)'"))
18501169689Skan    goto resync_fail;
18502169689Skan
18503169689Skan  check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
18504169689Skan  OMP_CLAUSE_CHAIN (c) = list;
18505169689Skan  return c;
18506169689Skan
18507169689Skan invalid_kind:
18508169689Skan  cp_parser_error (parser, "invalid schedule kind");
18509169689Skan resync_fail:
18510169689Skan  cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18511169689Skan					 /*or_comma=*/false,
18512169689Skan					 /*consume_paren=*/true);
18513169689Skan  return list;
18514169689Skan}
18515169689Skan
18516169689Skan/* Parse all OpenMP clauses.  The set clauses allowed by the directive
18517169689Skan   is a bitmask in MASK.  Return the list of clauses found; the result
18518169689Skan   of clause default goes in *pdefault.  */
18519169689Skan
18520169689Skanstatic tree
18521169689Skancp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
18522169689Skan			   const char *where, cp_token *pragma_tok)
18523169689Skan{
18524169689Skan  tree clauses = NULL;
18525169689Skan
18526169689Skan  while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
18527169689Skan    {
18528169689Skan      pragma_omp_clause c_kind = cp_parser_omp_clause_name (parser);
18529169689Skan      const char *c_name;
18530169689Skan      tree prev = clauses;
18531169689Skan
18532169689Skan      switch (c_kind)
18533169689Skan	{
18534169689Skan	case PRAGMA_OMP_CLAUSE_COPYIN:
18535169689Skan	  clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
18536169689Skan	  c_name = "copyin";
18537169689Skan	  break;
18538169689Skan	case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
18539169689Skan	  clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
18540169689Skan					    clauses);
18541169689Skan	  c_name = "copyprivate";
18542169689Skan	  break;
18543169689Skan	case PRAGMA_OMP_CLAUSE_DEFAULT:
18544169689Skan	  clauses = cp_parser_omp_clause_default (parser, clauses);
18545169689Skan	  c_name = "default";
18546169689Skan	  break;
18547169689Skan	case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
18548169689Skan	  clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
18549169689Skan					    clauses);
18550169689Skan	  c_name = "firstprivate";
18551169689Skan	  break;
18552169689Skan	case PRAGMA_OMP_CLAUSE_IF:
18553169689Skan	  clauses = cp_parser_omp_clause_if (parser, clauses);
18554169689Skan	  c_name = "if";
18555169689Skan	  break;
18556169689Skan	case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
18557169689Skan	  clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
18558169689Skan					    clauses);
18559169689Skan	  c_name = "lastprivate";
18560169689Skan	  break;
18561169689Skan	case PRAGMA_OMP_CLAUSE_NOWAIT:
18562169689Skan	  clauses = cp_parser_omp_clause_nowait (parser, clauses);
18563169689Skan	  c_name = "nowait";
18564169689Skan	  break;
18565169689Skan	case PRAGMA_OMP_CLAUSE_NUM_THREADS:
18566169689Skan	  clauses = cp_parser_omp_clause_num_threads (parser, clauses);
18567169689Skan	  c_name = "num_threads";
18568169689Skan	  break;
18569169689Skan	case PRAGMA_OMP_CLAUSE_ORDERED:
18570169689Skan	  clauses = cp_parser_omp_clause_ordered (parser, clauses);
18571169689Skan	  c_name = "ordered";
18572169689Skan	  break;
18573169689Skan	case PRAGMA_OMP_CLAUSE_PRIVATE:
18574169689Skan	  clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
18575169689Skan					    clauses);
18576169689Skan	  c_name = "private";
18577169689Skan	  break;
18578169689Skan	case PRAGMA_OMP_CLAUSE_REDUCTION:
18579169689Skan	  clauses = cp_parser_omp_clause_reduction (parser, clauses);
18580169689Skan	  c_name = "reduction";
18581169689Skan	  break;
18582169689Skan	case PRAGMA_OMP_CLAUSE_SCHEDULE:
18583169689Skan	  clauses = cp_parser_omp_clause_schedule (parser, clauses);
18584169689Skan	  c_name = "schedule";
18585169689Skan	  break;
18586169689Skan	case PRAGMA_OMP_CLAUSE_SHARED:
18587169689Skan	  clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
18588169689Skan					    clauses);
18589169689Skan	  c_name = "shared";
18590169689Skan	  break;
18591169689Skan	default:
18592169689Skan	  cp_parser_error (parser, "expected %<#pragma omp%> clause");
18593169689Skan	  goto saw_error;
18594169689Skan	}
18595169689Skan
18596169689Skan      if (((mask >> c_kind) & 1) == 0)
18597169689Skan	{
18598169689Skan	  /* Remove the invalid clause(s) from the list to avoid
18599169689Skan	     confusing the rest of the compiler.  */
18600169689Skan	  clauses = prev;
18601169689Skan	  error ("%qs is not valid for %qs", c_name, where);
18602169689Skan	}
18603169689Skan    }
18604169689Skan saw_error:
18605169689Skan  cp_parser_skip_to_pragma_eol (parser, pragma_tok);
18606169689Skan  return finish_omp_clauses (clauses);
18607169689Skan}
18608169689Skan
18609169689Skan/* OpenMP 2.5:
18610169689Skan   structured-block:
18611169689Skan     statement
18612169689Skan
18613169689Skan   In practice, we're also interested in adding the statement to an
18614169689Skan   outer node.  So it is convenient if we work around the fact that
18615169689Skan   cp_parser_statement calls add_stmt.  */
18616169689Skan
18617169689Skanstatic unsigned
18618169689Skancp_parser_begin_omp_structured_block (cp_parser *parser)
18619169689Skan{
18620169689Skan  unsigned save = parser->in_statement;
18621169689Skan
18622169689Skan  /* Only move the values to IN_OMP_BLOCK if they weren't false.
18623169689Skan     This preserves the "not within loop or switch" style error messages
18624169689Skan     for nonsense cases like
18625169689Skan	void foo() {
18626169689Skan	#pragma omp single
18627169689Skan	  break;
18628169689Skan	}
18629169689Skan  */
18630169689Skan  if (parser->in_statement)
18631169689Skan    parser->in_statement = IN_OMP_BLOCK;
18632169689Skan
18633169689Skan  return save;
18634169689Skan}
18635169689Skan
18636169689Skanstatic void
18637169689Skancp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
18638169689Skan{
18639169689Skan  parser->in_statement = save;
18640169689Skan}
18641169689Skan
18642169689Skanstatic tree
18643169689Skancp_parser_omp_structured_block (cp_parser *parser)
18644169689Skan{
18645169689Skan  tree stmt = begin_omp_structured_block ();
18646169689Skan  unsigned int save = cp_parser_begin_omp_structured_block (parser);
18647169689Skan
18648169689Skan  cp_parser_statement (parser, NULL_TREE, false);
18649169689Skan
18650169689Skan  cp_parser_end_omp_structured_block (parser, save);
18651169689Skan  return finish_omp_structured_block (stmt);
18652169689Skan}
18653169689Skan
18654169689Skan/* OpenMP 2.5:
18655169689Skan   # pragma omp atomic new-line
18656169689Skan     expression-stmt
18657169689Skan
18658169689Skan   expression-stmt:
18659169689Skan     x binop= expr | x++ | ++x | x-- | --x
18660169689Skan   binop:
18661169689Skan     +, *, -, /, &, ^, |, <<, >>
18662169689Skan
18663169689Skan  where x is an lvalue expression with scalar type.  */
18664169689Skan
18665169689Skanstatic void
18666169689Skancp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
18667169689Skan{
18668169689Skan  tree lhs, rhs;
18669169689Skan  enum tree_code code;
18670169689Skan
18671169689Skan  cp_parser_require_pragma_eol (parser, pragma_tok);
18672169689Skan
18673169689Skan  lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
18674169689Skan				    /*cast_p=*/false);
18675169689Skan  switch (TREE_CODE (lhs))
18676169689Skan    {
18677169689Skan    case ERROR_MARK:
18678169689Skan      goto saw_error;
18679169689Skan
18680169689Skan    case PREINCREMENT_EXPR:
18681169689Skan    case POSTINCREMENT_EXPR:
18682169689Skan      lhs = TREE_OPERAND (lhs, 0);
18683169689Skan      code = PLUS_EXPR;
18684169689Skan      rhs = integer_one_node;
18685169689Skan      break;
18686169689Skan
18687169689Skan    case PREDECREMENT_EXPR:
18688169689Skan    case POSTDECREMENT_EXPR:
18689169689Skan      lhs = TREE_OPERAND (lhs, 0);
18690169689Skan      code = MINUS_EXPR;
18691169689Skan      rhs = integer_one_node;
18692169689Skan      break;
18693169689Skan
18694169689Skan    default:
18695169689Skan      switch (cp_lexer_peek_token (parser->lexer)->type)
18696169689Skan	{
18697169689Skan	case CPP_MULT_EQ:
18698169689Skan	  code = MULT_EXPR;
18699169689Skan	  break;
18700169689Skan	case CPP_DIV_EQ:
18701169689Skan	  code = TRUNC_DIV_EXPR;
18702169689Skan	  break;
18703169689Skan	case CPP_PLUS_EQ:
18704169689Skan	  code = PLUS_EXPR;
18705169689Skan	  break;
18706169689Skan	case CPP_MINUS_EQ:
18707169689Skan	  code = MINUS_EXPR;
18708169689Skan	  break;
18709169689Skan	case CPP_LSHIFT_EQ:
18710169689Skan	  code = LSHIFT_EXPR;
18711169689Skan	  break;
18712169689Skan	case CPP_RSHIFT_EQ:
18713169689Skan	  code = RSHIFT_EXPR;
18714169689Skan	  break;
18715169689Skan	case CPP_AND_EQ:
18716169689Skan	  code = BIT_AND_EXPR;
18717169689Skan	  break;
18718169689Skan	case CPP_OR_EQ:
18719169689Skan	  code = BIT_IOR_EXPR;
18720169689Skan	  break;
18721169689Skan	case CPP_XOR_EQ:
18722169689Skan	  code = BIT_XOR_EXPR;
18723169689Skan	  break;
18724169689Skan	default:
18725169689Skan	  cp_parser_error (parser,
18726169689Skan			   "invalid operator for %<#pragma omp atomic%>");
18727169689Skan	  goto saw_error;
18728169689Skan	}
18729169689Skan      cp_lexer_consume_token (parser->lexer);
18730169689Skan
18731169689Skan      rhs = cp_parser_expression (parser, false);
18732169689Skan      if (rhs == error_mark_node)
18733169689Skan	goto saw_error;
18734169689Skan      break;
18735169689Skan    }
18736169689Skan  finish_omp_atomic (code, lhs, rhs);
18737169689Skan  cp_parser_consume_semicolon_at_end_of_statement (parser);
18738169689Skan  return;
18739169689Skan
18740169689Skan saw_error:
18741169689Skan  cp_parser_skip_to_end_of_block_or_statement (parser);
18742169689Skan}
18743169689Skan
18744169689Skan
18745169689Skan/* OpenMP 2.5:
18746169689Skan   # pragma omp barrier new-line  */
18747169689Skan
18748169689Skanstatic void
18749169689Skancp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
18750169689Skan{
18751169689Skan  cp_parser_require_pragma_eol (parser, pragma_tok);
18752169689Skan  finish_omp_barrier ();
18753169689Skan}
18754169689Skan
18755169689Skan/* OpenMP 2.5:
18756169689Skan   # pragma omp critical [(name)] new-line
18757169689Skan     structured-block  */
18758169689Skan
18759169689Skanstatic tree
18760169689Skancp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
18761169689Skan{
18762169689Skan  tree stmt, name = NULL;
18763169689Skan
18764169689Skan  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18765169689Skan    {
18766169689Skan      cp_lexer_consume_token (parser->lexer);
18767169689Skan
18768169689Skan      name = cp_parser_identifier (parser);
18769169689Skan
18770169689Skan      if (name == error_mark_node
18771169689Skan	  || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18772169689Skan	cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18773169689Skan					       /*or_comma=*/false,
18774169689Skan					       /*consume_paren=*/true);
18775169689Skan      if (name == error_mark_node)
18776169689Skan	name = NULL;
18777169689Skan    }
18778169689Skan  cp_parser_require_pragma_eol (parser, pragma_tok);
18779169689Skan
18780169689Skan  stmt = cp_parser_omp_structured_block (parser);
18781169689Skan  return c_finish_omp_critical (stmt, name);
18782169689Skan}
18783169689Skan
18784169689Skan/* OpenMP 2.5:
18785169689Skan   # pragma omp flush flush-vars[opt] new-line
18786169689Skan
18787169689Skan   flush-vars:
18788169689Skan     ( variable-list ) */
18789169689Skan
18790169689Skanstatic void
18791169689Skancp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
18792169689Skan{
18793169689Skan  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18794169689Skan    (void) cp_parser_omp_var_list (parser, 0, NULL);
18795169689Skan  cp_parser_require_pragma_eol (parser, pragma_tok);
18796169689Skan
18797169689Skan  finish_omp_flush ();
18798169689Skan}
18799169689Skan
18800169689Skan/* Parse the restricted form of the for statment allowed by OpenMP.  */
18801169689Skan
18802169689Skanstatic tree
18803169689Skancp_parser_omp_for_loop (cp_parser *parser)
18804169689Skan{
18805169689Skan  tree init, cond, incr, body, decl, pre_body;
18806169689Skan  location_t loc;
18807169689Skan
18808169689Skan  if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
18809169689Skan    {
18810169689Skan      cp_parser_error (parser, "for statement expected");
18811169689Skan      return NULL;
18812169689Skan    }
18813169689Skan  loc = cp_lexer_consume_token (parser->lexer)->location;
18814169689Skan  if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18815169689Skan    return NULL;
18816169689Skan
18817169689Skan  init = decl = NULL;
18818169689Skan  pre_body = push_stmt_list ();
18819169689Skan  if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18820169689Skan    {
18821169689Skan      cp_decl_specifier_seq type_specifiers;
18822169689Skan
18823169689Skan      /* First, try to parse as an initialized declaration.  See
18824169689Skan	 cp_parser_condition, from whence the bulk of this is copied.  */
18825169689Skan
18826169689Skan      cp_parser_parse_tentatively (parser);
18827169689Skan      cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
18828169689Skan				    &type_specifiers);
18829169689Skan      if (!cp_parser_error_occurred (parser))
18830169689Skan	{
18831169689Skan	  tree asm_specification, attributes;
18832169689Skan	  cp_declarator *declarator;
18833169689Skan
18834169689Skan	  declarator = cp_parser_declarator (parser,
18835169689Skan					     CP_PARSER_DECLARATOR_NAMED,
18836169689Skan					     /*ctor_dtor_or_conv_p=*/NULL,
18837169689Skan					     /*parenthesized_p=*/NULL,
18838169689Skan					     /*member_p=*/false);
18839169689Skan	  attributes = cp_parser_attributes_opt (parser);
18840169689Skan	  asm_specification = cp_parser_asm_specification_opt (parser);
18841169689Skan
18842169689Skan	  cp_parser_require (parser, CPP_EQ, "`='");
18843169689Skan	  if (cp_parser_parse_definitely (parser))
18844169689Skan	    {
18845169689Skan	      tree pushed_scope;
18846169689Skan
18847169689Skan	      decl = start_decl (declarator, &type_specifiers,
18848169689Skan				 /*initialized_p=*/false, attributes,
18849169689Skan				 /*prefix_attributes=*/NULL_TREE,
18850169689Skan				 &pushed_scope);
18851169689Skan
18852169689Skan	      init = cp_parser_assignment_expression (parser, false);
18853169689Skan
18854169689Skan	      cp_finish_decl (decl, NULL_TREE, /*init_const_expr_p=*/false,
18855169689Skan			      asm_specification, LOOKUP_ONLYCONVERTING);
18856169689Skan
18857169689Skan	      if (pushed_scope)
18858169689Skan		pop_scope (pushed_scope);
18859169689Skan	    }
18860169689Skan	}
18861169689Skan      else
18862169689Skan	cp_parser_abort_tentative_parse (parser);
18863169689Skan
18864169689Skan      /* If parsing as an initialized declaration failed, try again as
18865169689Skan	 a simple expression.  */
18866169689Skan      if (decl == NULL)
18867169689Skan	init = cp_parser_expression (parser, false);
18868169689Skan    }
18869169689Skan  cp_parser_require (parser, CPP_SEMICOLON, "`;'");
18870169689Skan  pre_body = pop_stmt_list (pre_body);
18871169689Skan
18872169689Skan  cond = NULL;
18873169689Skan  if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18874169689Skan    cond = cp_parser_condition (parser);
18875169689Skan  cp_parser_require (parser, CPP_SEMICOLON, "`;'");
18876169689Skan
18877169689Skan  incr = NULL;
18878169689Skan  if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
18879169689Skan    incr = cp_parser_expression (parser, false);
18880169689Skan
18881169689Skan  if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18882169689Skan    cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18883169689Skan					   /*or_comma=*/false,
18884169689Skan					   /*consume_paren=*/true);
18885169689Skan
18886169689Skan  /* Note that we saved the original contents of this flag when we entered
18887169689Skan     the structured block, and so we don't need to re-save it here.  */
18888169689Skan  parser->in_statement = IN_OMP_FOR;
18889169689Skan
18890169689Skan  /* Note that the grammar doesn't call for a structured block here,
18891169689Skan     though the loop as a whole is a structured block.  */
18892169689Skan  body = push_stmt_list ();
18893169689Skan  cp_parser_statement (parser, NULL_TREE, false);
18894169689Skan  body = pop_stmt_list (body);
18895169689Skan
18896169689Skan  return finish_omp_for (loc, decl, init, cond, incr, body, pre_body);
18897169689Skan}
18898169689Skan
18899169689Skan/* OpenMP 2.5:
18900169689Skan   #pragma omp for for-clause[optseq] new-line
18901169689Skan     for-loop  */
18902169689Skan
18903169689Skan#define OMP_FOR_CLAUSE_MASK				\
18904169689Skan	( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)		\
18905169689Skan	| (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)	\
18906169689Skan	| (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)		\
18907169689Skan	| (1u << PRAGMA_OMP_CLAUSE_REDUCTION)		\
18908169689Skan	| (1u << PRAGMA_OMP_CLAUSE_ORDERED)		\
18909169689Skan	| (1u << PRAGMA_OMP_CLAUSE_SCHEDULE)		\
18910169689Skan	| (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
18911169689Skan
18912169689Skanstatic tree
18913169689Skancp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
18914169689Skan{
18915169689Skan  tree clauses, sb, ret;
18916169689Skan  unsigned int save;
18917169689Skan
18918169689Skan  clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
18919169689Skan				       "#pragma omp for", pragma_tok);
18920169689Skan
18921169689Skan  sb = begin_omp_structured_block ();
18922169689Skan  save = cp_parser_begin_omp_structured_block (parser);
18923169689Skan
18924169689Skan  ret = cp_parser_omp_for_loop (parser);
18925169689Skan  if (ret)
18926169689Skan    OMP_FOR_CLAUSES (ret) = clauses;
18927169689Skan
18928169689Skan  cp_parser_end_omp_structured_block (parser, save);
18929169689Skan  add_stmt (finish_omp_structured_block (sb));
18930169689Skan
18931169689Skan  return ret;
18932169689Skan}
18933169689Skan
18934169689Skan/* OpenMP 2.5:
18935169689Skan   # pragma omp master new-line
18936169689Skan     structured-block  */
18937169689Skan
18938169689Skanstatic tree
18939169689Skancp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
18940169689Skan{
18941169689Skan  cp_parser_require_pragma_eol (parser, pragma_tok);
18942169689Skan  return c_finish_omp_master (cp_parser_omp_structured_block (parser));
18943169689Skan}
18944169689Skan
18945169689Skan/* OpenMP 2.5:
18946169689Skan   # pragma omp ordered new-line
18947169689Skan     structured-block  */
18948169689Skan
18949169689Skanstatic tree
18950169689Skancp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
18951169689Skan{
18952169689Skan  cp_parser_require_pragma_eol (parser, pragma_tok);
18953169689Skan  return c_finish_omp_ordered (cp_parser_omp_structured_block (parser));
18954169689Skan}
18955169689Skan
18956169689Skan/* OpenMP 2.5:
18957169689Skan
18958169689Skan   section-scope:
18959169689Skan     { section-sequence }
18960169689Skan
18961169689Skan   section-sequence:
18962169689Skan     section-directive[opt] structured-block
18963169689Skan     section-sequence section-directive structured-block  */
18964169689Skan
18965169689Skanstatic tree
18966169689Skancp_parser_omp_sections_scope (cp_parser *parser)
18967169689Skan{
18968169689Skan  tree stmt, substmt;
18969169689Skan  bool error_suppress = false;
18970169689Skan  cp_token *tok;
18971169689Skan
18972169689Skan  if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
18973169689Skan    return NULL_TREE;
18974169689Skan
18975169689Skan  stmt = push_stmt_list ();
18976169689Skan
18977169689Skan  if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
18978169689Skan    {
18979169689Skan      unsigned save;
18980169689Skan
18981169689Skan      substmt = begin_omp_structured_block ();
18982169689Skan      save = cp_parser_begin_omp_structured_block (parser);
18983169689Skan
18984169689Skan      while (1)
18985169689Skan	{
18986169689Skan	  cp_parser_statement (parser, NULL_TREE, false);
18987169689Skan
18988169689Skan	  tok = cp_lexer_peek_token (parser->lexer);
18989169689Skan	  if (tok->pragma_kind == PRAGMA_OMP_SECTION)
18990169689Skan	    break;
18991169689Skan	  if (tok->type == CPP_CLOSE_BRACE)
18992169689Skan	    break;
18993169689Skan	  if (tok->type == CPP_EOF)
18994169689Skan	    break;
18995169689Skan	}
18996169689Skan
18997169689Skan      cp_parser_end_omp_structured_block (parser, save);
18998169689Skan      substmt = finish_omp_structured_block (substmt);
18999169689Skan      substmt = build1 (OMP_SECTION, void_type_node, substmt);
19000169689Skan      add_stmt (substmt);
19001169689Skan    }
19002169689Skan
19003169689Skan  while (1)
19004169689Skan    {
19005169689Skan      tok = cp_lexer_peek_token (parser->lexer);
19006169689Skan      if (tok->type == CPP_CLOSE_BRACE)
19007169689Skan	break;
19008169689Skan      if (tok->type == CPP_EOF)
19009169689Skan	break;
19010169689Skan
19011169689Skan      if (tok->pragma_kind == PRAGMA_OMP_SECTION)
19012169689Skan	{
19013169689Skan	  cp_lexer_consume_token (parser->lexer);
19014169689Skan	  cp_parser_require_pragma_eol (parser, tok);
19015169689Skan	  error_suppress = false;
19016169689Skan	}
19017169689Skan      else if (!error_suppress)
19018169689Skan	{
19019169689Skan	  cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
19020169689Skan	  error_suppress = true;
19021169689Skan	}
19022169689Skan
19023169689Skan      substmt = cp_parser_omp_structured_block (parser);
19024169689Skan      substmt = build1 (OMP_SECTION, void_type_node, substmt);
19025169689Skan      add_stmt (substmt);
19026169689Skan    }
19027169689Skan  cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
19028169689Skan
19029169689Skan  substmt = pop_stmt_list (stmt);
19030169689Skan
19031169689Skan  stmt = make_node (OMP_SECTIONS);
19032169689Skan  TREE_TYPE (stmt) = void_type_node;
19033169689Skan  OMP_SECTIONS_BODY (stmt) = substmt;
19034169689Skan
19035169689Skan  add_stmt (stmt);
19036169689Skan  return stmt;
19037169689Skan}
19038169689Skan
19039169689Skan/* OpenMP 2.5:
19040169689Skan   # pragma omp sections sections-clause[optseq] newline
19041169689Skan     sections-scope  */
19042169689Skan
19043169689Skan#define OMP_SECTIONS_CLAUSE_MASK			\
19044169689Skan	( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)		\
19045169689Skan	| (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)	\
19046169689Skan	| (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)		\
19047169689Skan	| (1u << PRAGMA_OMP_CLAUSE_REDUCTION)		\
19048169689Skan	| (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
19049169689Skan
19050169689Skanstatic tree
19051169689Skancp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
19052169689Skan{
19053169689Skan  tree clauses, ret;
19054169689Skan
19055169689Skan  clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
19056169689Skan				       "#pragma omp sections", pragma_tok);
19057169689Skan
19058169689Skan  ret = cp_parser_omp_sections_scope (parser);
19059169689Skan  if (ret)
19060169689Skan    OMP_SECTIONS_CLAUSES (ret) = clauses;
19061169689Skan
19062169689Skan  return ret;
19063169689Skan}
19064169689Skan
19065169689Skan/* OpenMP 2.5:
19066169689Skan   # pragma parallel parallel-clause new-line
19067169689Skan   # pragma parallel for parallel-for-clause new-line
19068169689Skan   # pragma parallel sections parallel-sections-clause new-line  */
19069169689Skan
19070169689Skan#define OMP_PARALLEL_CLAUSE_MASK			\
19071169689Skan	( (1u << PRAGMA_OMP_CLAUSE_IF)			\
19072169689Skan	| (1u << PRAGMA_OMP_CLAUSE_PRIVATE)		\
19073169689Skan	| (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)	\
19074169689Skan	| (1u << PRAGMA_OMP_CLAUSE_DEFAULT)		\
19075169689Skan	| (1u << PRAGMA_OMP_CLAUSE_SHARED)		\
19076169689Skan	| (1u << PRAGMA_OMP_CLAUSE_COPYIN)		\
19077169689Skan	| (1u << PRAGMA_OMP_CLAUSE_REDUCTION)		\
19078169689Skan	| (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
19079169689Skan
19080169689Skanstatic tree
19081169689Skancp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
19082169689Skan{
19083169689Skan  enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
19084169689Skan  const char *p_name = "#pragma omp parallel";
19085169689Skan  tree stmt, clauses, par_clause, ws_clause, block;
19086169689Skan  unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
19087169689Skan  unsigned int save;
19088169689Skan
19089169689Skan  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
19090169689Skan    {
19091169689Skan      cp_lexer_consume_token (parser->lexer);
19092169689Skan      p_kind = PRAGMA_OMP_PARALLEL_FOR;
19093169689Skan      p_name = "#pragma omp parallel for";
19094169689Skan      mask |= OMP_FOR_CLAUSE_MASK;
19095169689Skan      mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
19096169689Skan    }
19097169689Skan  else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19098169689Skan    {
19099169689Skan      tree id = cp_lexer_peek_token (parser->lexer)->u.value;
19100169689Skan      const char *p = IDENTIFIER_POINTER (id);
19101169689Skan      if (strcmp (p, "sections") == 0)
19102169689Skan	{
19103169689Skan	  cp_lexer_consume_token (parser->lexer);
19104169689Skan	  p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
19105169689Skan	  p_name = "#pragma omp parallel sections";
19106169689Skan	  mask |= OMP_SECTIONS_CLAUSE_MASK;
19107169689Skan	  mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
19108169689Skan	}
19109169689Skan    }
19110169689Skan
19111169689Skan  clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
19112169689Skan  block = begin_omp_parallel ();
19113169689Skan  save = cp_parser_begin_omp_structured_block (parser);
19114169689Skan
19115169689Skan  switch (p_kind)
19116169689Skan    {
19117169689Skan    case PRAGMA_OMP_PARALLEL:
19118169689Skan      cp_parser_already_scoped_statement (parser);
19119169689Skan      par_clause = clauses;
19120169689Skan      break;
19121169689Skan
19122169689Skan    case PRAGMA_OMP_PARALLEL_FOR:
19123169689Skan      c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
19124169689Skan      stmt = cp_parser_omp_for_loop (parser);
19125169689Skan      if (stmt)
19126169689Skan	OMP_FOR_CLAUSES (stmt) = ws_clause;
19127169689Skan      break;
19128169689Skan
19129169689Skan    case PRAGMA_OMP_PARALLEL_SECTIONS:
19130169689Skan      c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
19131169689Skan      stmt = cp_parser_omp_sections_scope (parser);
19132169689Skan      if (stmt)
19133169689Skan	OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
19134169689Skan      break;
19135169689Skan
19136169689Skan    default:
19137169689Skan      gcc_unreachable ();
19138169689Skan    }
19139169689Skan
19140169689Skan  cp_parser_end_omp_structured_block (parser, save);
19141169689Skan  stmt = finish_omp_parallel (par_clause, block);
19142169689Skan  if (p_kind != PRAGMA_OMP_PARALLEL)
19143169689Skan    OMP_PARALLEL_COMBINED (stmt) = 1;
19144169689Skan  return stmt;
19145169689Skan}
19146169689Skan
19147169689Skan/* OpenMP 2.5:
19148169689Skan   # pragma omp single single-clause[optseq] new-line
19149169689Skan     structured-block  */
19150169689Skan
19151169689Skan#define OMP_SINGLE_CLAUSE_MASK				\
19152169689Skan	( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)		\
19153169689Skan	| (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)	\
19154169689Skan	| (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE)		\
19155169689Skan	| (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
19156169689Skan
19157169689Skanstatic tree
19158169689Skancp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
19159169689Skan{
19160169689Skan  tree stmt = make_node (OMP_SINGLE);
19161169689Skan  TREE_TYPE (stmt) = void_type_node;
19162169689Skan
19163169689Skan  OMP_SINGLE_CLAUSES (stmt)
19164169689Skan    = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
19165169689Skan				 "#pragma omp single", pragma_tok);
19166169689Skan  OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
19167169689Skan
19168169689Skan  return add_stmt (stmt);
19169169689Skan}
19170169689Skan
19171169689Skan/* OpenMP 2.5:
19172169689Skan   # pragma omp threadprivate (variable-list) */
19173169689Skan
19174169689Skanstatic void
19175169689Skancp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
19176169689Skan{
19177169689Skan  tree vars;
19178169689Skan
19179169689Skan  vars = cp_parser_omp_var_list (parser, 0, NULL);
19180169689Skan  cp_parser_require_pragma_eol (parser, pragma_tok);
19181169689Skan
19182169689Skan  if (!targetm.have_tls)
19183169689Skan    sorry ("threadprivate variables not supported in this target");
19184169689Skan
19185169689Skan  finish_omp_threadprivate (vars);
19186169689Skan}
19187169689Skan
19188169689Skan/* Main entry point to OpenMP statement pragmas.  */
19189169689Skan
19190169689Skanstatic void
19191169689Skancp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
19192169689Skan{
19193169689Skan  tree stmt;
19194169689Skan
19195169689Skan  switch (pragma_tok->pragma_kind)
19196169689Skan    {
19197169689Skan    case PRAGMA_OMP_ATOMIC:
19198169689Skan      cp_parser_omp_atomic (parser, pragma_tok);
19199169689Skan      return;
19200169689Skan    case PRAGMA_OMP_CRITICAL:
19201169689Skan      stmt = cp_parser_omp_critical (parser, pragma_tok);
19202169689Skan      break;
19203169689Skan    case PRAGMA_OMP_FOR:
19204169689Skan      stmt = cp_parser_omp_for (parser, pragma_tok);
19205169689Skan      break;
19206169689Skan    case PRAGMA_OMP_MASTER:
19207169689Skan      stmt = cp_parser_omp_master (parser, pragma_tok);
19208169689Skan      break;
19209169689Skan    case PRAGMA_OMP_ORDERED:
19210169689Skan      stmt = cp_parser_omp_ordered (parser, pragma_tok);
19211169689Skan      break;
19212169689Skan    case PRAGMA_OMP_PARALLEL:
19213169689Skan      stmt = cp_parser_omp_parallel (parser, pragma_tok);
19214169689Skan      break;
19215169689Skan    case PRAGMA_OMP_SECTIONS:
19216169689Skan      stmt = cp_parser_omp_sections (parser, pragma_tok);
19217169689Skan      break;
19218169689Skan    case PRAGMA_OMP_SINGLE:
19219169689Skan      stmt = cp_parser_omp_single (parser, pragma_tok);
19220169689Skan      break;
19221169689Skan    default:
19222169689Skan      gcc_unreachable ();
19223169689Skan    }
19224169689Skan
19225169689Skan  if (stmt)
19226169689Skan    SET_EXPR_LOCATION (stmt, pragma_tok->location);
19227169689Skan}
19228169689Skan
19229132718Skan/* The parser.  */
19230132718Skan
19231132718Skanstatic GTY (()) cp_parser *the_parser;
19232132718Skan
19233169689Skan
19234169689Skan/* Special handling for the first token or line in the file.  The first
19235169689Skan   thing in the file might be #pragma GCC pch_preprocess, which loads a
19236169689Skan   PCH file, which is a GC collection point.  So we need to handle this
19237169689Skan   first pragma without benefit of an existing lexer structure.
19238169689Skan
19239169689Skan   Always returns one token to the caller in *FIRST_TOKEN.  This is
19240169689Skan   either the true first token of the file, or the first token after
19241169689Skan   the initial pragma.  */
19242169689Skan
19243169689Skanstatic void
19244169689Skancp_parser_initial_pragma (cp_token *first_token)
19245169689Skan{
19246169689Skan  tree name = NULL;
19247169689Skan
19248169689Skan  cp_lexer_get_preprocessor_token (NULL, first_token);
19249169689Skan  if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
19250169689Skan    return;
19251169689Skan
19252169689Skan  cp_lexer_get_preprocessor_token (NULL, first_token);
19253169689Skan  if (first_token->type == CPP_STRING)
19254169689Skan    {
19255169689Skan      name = first_token->u.value;
19256169689Skan
19257169689Skan      cp_lexer_get_preprocessor_token (NULL, first_token);
19258169689Skan      if (first_token->type != CPP_PRAGMA_EOL)
19259169689Skan	error ("junk at end of %<#pragma GCC pch_preprocess%>");
19260169689Skan    }
19261169689Skan  else
19262169689Skan    error ("expected string literal");
19263169689Skan
19264169689Skan  /* Skip to the end of the pragma.  */
19265169689Skan  while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
19266169689Skan    cp_lexer_get_preprocessor_token (NULL, first_token);
19267169689Skan
19268169689Skan  /* Now actually load the PCH file.  */
19269169689Skan  if (name)
19270169689Skan    c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
19271169689Skan
19272169689Skan  /* Read one more token to return to our caller.  We have to do this
19273169689Skan     after reading the PCH file in, since its pointers have to be
19274169689Skan     live.  */
19275169689Skan  cp_lexer_get_preprocessor_token (NULL, first_token);
19276169689Skan}
19277169689Skan
19278169689Skan/* Normal parsing of a pragma token.  Here we can (and must) use the
19279169689Skan   regular lexer.  */
19280169689Skan
19281169689Skanstatic bool
19282169689Skancp_parser_pragma (cp_parser *parser, enum pragma_context context)
19283169689Skan{
19284169689Skan  cp_token *pragma_tok;
19285169689Skan  unsigned int id;
19286169689Skan
19287169689Skan  pragma_tok = cp_lexer_consume_token (parser->lexer);
19288169689Skan  gcc_assert (pragma_tok->type == CPP_PRAGMA);
19289169689Skan  parser->lexer->in_pragma = true;
19290169689Skan
19291169689Skan  id = pragma_tok->pragma_kind;
19292169689Skan  switch (id)
19293169689Skan    {
19294169689Skan    case PRAGMA_GCC_PCH_PREPROCESS:
19295169689Skan      error ("%<#pragma GCC pch_preprocess%> must be first");
19296169689Skan      break;
19297169689Skan
19298169689Skan    case PRAGMA_OMP_BARRIER:
19299169689Skan      switch (context)
19300169689Skan	{
19301169689Skan	case pragma_compound:
19302169689Skan	  cp_parser_omp_barrier (parser, pragma_tok);
19303169689Skan	  return false;
19304169689Skan	case pragma_stmt:
19305169689Skan	  error ("%<#pragma omp barrier%> may only be "
19306169689Skan		 "used in compound statements");
19307169689Skan	  break;
19308169689Skan	default:
19309169689Skan	  goto bad_stmt;
19310169689Skan	}
19311169689Skan      break;
19312169689Skan
19313169689Skan    case PRAGMA_OMP_FLUSH:
19314169689Skan      switch (context)
19315169689Skan	{
19316169689Skan	case pragma_compound:
19317169689Skan	  cp_parser_omp_flush (parser, pragma_tok);
19318169689Skan	  return false;
19319169689Skan	case pragma_stmt:
19320169689Skan	  error ("%<#pragma omp flush%> may only be "
19321169689Skan		 "used in compound statements");
19322169689Skan	  break;
19323169689Skan	default:
19324169689Skan	  goto bad_stmt;
19325169689Skan	}
19326169689Skan      break;
19327169689Skan
19328169689Skan    case PRAGMA_OMP_THREADPRIVATE:
19329169689Skan      cp_parser_omp_threadprivate (parser, pragma_tok);
19330169689Skan      return false;
19331169689Skan
19332169689Skan    case PRAGMA_OMP_ATOMIC:
19333169689Skan    case PRAGMA_OMP_CRITICAL:
19334169689Skan    case PRAGMA_OMP_FOR:
19335169689Skan    case PRAGMA_OMP_MASTER:
19336169689Skan    case PRAGMA_OMP_ORDERED:
19337169689Skan    case PRAGMA_OMP_PARALLEL:
19338169689Skan    case PRAGMA_OMP_SECTIONS:
19339169689Skan    case PRAGMA_OMP_SINGLE:
19340169689Skan      if (context == pragma_external)
19341169689Skan	goto bad_stmt;
19342169689Skan      cp_parser_omp_construct (parser, pragma_tok);
19343169689Skan      return true;
19344169689Skan
19345169689Skan    case PRAGMA_OMP_SECTION:
19346169689Skan      error ("%<#pragma omp section%> may only be used in "
19347169689Skan	     "%<#pragma omp sections%> construct");
19348169689Skan      break;
19349169689Skan
19350169689Skan    default:
19351169689Skan      gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
19352169689Skan      c_invoke_pragma_handler (id);
19353169689Skan      break;
19354169689Skan
19355169689Skan    bad_stmt:
19356169689Skan      cp_parser_error (parser, "expected declaration specifiers");
19357169689Skan      break;
19358169689Skan    }
19359169689Skan
19360169689Skan  cp_parser_skip_to_pragma_eol (parser, pragma_tok);
19361169689Skan  return false;
19362169689Skan}
19363169689Skan
19364169689Skan/* The interface the pragma parsers have to the lexer.  */
19365169689Skan
19366169689Skanenum cpp_ttype
19367169689Skanpragma_lex (tree *value)
19368169689Skan{
19369169689Skan  cp_token *tok;
19370169689Skan  enum cpp_ttype ret;
19371169689Skan
19372169689Skan  tok = cp_lexer_peek_token (the_parser->lexer);
19373169689Skan
19374169689Skan  ret = tok->type;
19375169689Skan  *value = tok->u.value;
19376169689Skan
19377169689Skan  if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
19378169689Skan    ret = CPP_EOF;
19379169689Skan  else if (ret == CPP_STRING)
19380169689Skan    *value = cp_parser_string_literal (the_parser, false, false);
19381169689Skan  else
19382169689Skan    {
19383169689Skan      cp_lexer_consume_token (the_parser->lexer);
19384169689Skan      if (ret == CPP_KEYWORD)
19385169689Skan	ret = CPP_NAME;
19386169689Skan    }
19387169689Skan
19388169689Skan  return ret;
19389169689Skan}
19390169689Skan
19391169689Skan
19392132718Skan/* External interface.  */
19393132718Skan
19394132718Skan/* Parse one entire translation unit.  */
19395132718Skan
19396132718Skanvoid
19397132718Skanc_parse_file (void)
19398132718Skan{
19399132718Skan  bool error_occurred;
19400169689Skan  static bool already_called = false;
19401132718Skan
19402169689Skan  if (already_called)
19403169689Skan    {
19404169689Skan      sorry ("inter-module optimizations not implemented for C++");
19405169689Skan      return;
19406169689Skan    }
19407169689Skan  already_called = true;
19408169689Skan
19409132718Skan  the_parser = cp_parser_new ();
19410132718Skan  push_deferring_access_checks (flag_access_control
19411132718Skan				? dk_no_deferred : dk_no_check);
19412132718Skan  error_occurred = cp_parser_translation_unit (the_parser);
19413132718Skan  the_parser = NULL;
19414132718Skan}
19415132718Skan
19416132718Skan/* This variable must be provided by every front end.  */
19417132718Skan
19418132718Skanint yydebug;
19419132718Skan
19420132718Skan#include "gt-cp-parser.h"
19421