xref: /openbsd/gnu/usr.bin/gcc/gcc/cp/spew.c (revision c87b03e5)
1 /* Type Analyzer for GNU C++.
2    Copyright (C) 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3    1999, 2000, 2001, 2002 Free Software Foundation, Inc.
4    Hacked... nay, bludgeoned... by Mark Eichin (eichin@cygnus.com)
5 
6 This file is part of GNU CC.
7 
8 GNU CC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
12 
13 GNU CC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 GNU General Public License for more details.
17 
18 You should have received a copy of the GNU General Public License
19 along with GNU CC; see the file COPYING.  If not, write to
20 the Free Software Foundation, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA.  */
22 
23 /* This file is the type analyzer for GNU C++.  To debug it, define SPEW_DEBUG
24    when compiling parse.c and spew.c.  */
25 
26 #include "config.h"
27 #include "system.h"
28 #include "input.h"
29 #include "tree.h"
30 #include "cp-tree.h"
31 #include "cpplib.h"
32 #include "c-pragma.h"
33 #include "lex.h"
34 #include "parse.h"
35 #include "flags.h"
36 #include "obstack.h"
37 #include "toplev.h"
38 #include "ggc.h"
39 #include "intl.h"
40 #include "timevar.h"
41 
42 #ifdef SPEW_DEBUG
43 #define SPEW_INLINE
44 #else
45 #define SPEW_INLINE inline
46 #endif
47 
48 /* This takes a token stream that hasn't decided much about types and
49    tries to figure out as much as it can, with excessive lookahead and
50    backtracking.  */
51 
52 /* fifo of tokens recognized and available to parser.  */
53 struct token GTY(())
54 {
55   /* The values for YYCHAR will fit in a short.  */
56   short		yychar;
57   unsigned int	lineno;
58   YYSTYPE GTY ((desc ("%1.yychar"))) yylval;
59 };
60 
61 /* Since inline methods can refer to text which has not yet been seen,
62    we store the text of the method in a structure which is placed in the
63    DECL_PENDING_INLINE_INFO field of the FUNCTION_DECL.
64    After parsing the body of the class definition, the FUNCTION_DECL's are
65    scanned to see which ones have this field set.  Those are then digested
66    one at a time.
67 
68    This function's FUNCTION_DECL will have a bit set in its common so
69    that we know to watch out for it.  */
70 
71 #define TOKEN_CHUNK_SIZE 20
72 struct token_chunk GTY(())
73 {
74   struct token_chunk *next;
75   struct token toks[TOKEN_CHUNK_SIZE];
76 };
77 
78 struct unparsed_text GTY(())
79 {
80   struct unparsed_text *next;	/* process this one next */
81   tree decl;		/* associated declaration */
82   location_t locus;     /* location we got the text from */
83   int interface;	/* remembering interface_unknown and interface_only */
84 
85   struct token_chunk * tokens; /* Start of the token list.  */
86 
87   struct token_chunk *last_chunk; /* End of the token list.  */
88   short last_pos;	/* Number of tokens used in the last chunk of
89 			   TOKENS.  */
90 
91   short cur_pos;	/* Current token in 'cur_chunk', when rescanning.  */
92   struct token_chunk *cur_chunk;  /* Current chunk, when rescanning.  */
93 };
94 
95 /* Stack of state saved off when we return to an inline method or
96    default argument that has been stored for later parsing.  */
97 struct feed GTY(())
98 {
99   struct unparsed_text *input;
100   location_t locus;
101   int yychar;
102   YYSTYPE GTY ((desc ("%1.yychar"))) yylval;
103   int first_token;
104   struct obstack GTY ((skip (""))) token_obstack;
105   struct feed *next;
106 };
107 
108 static GTY(()) struct feed *feed;
109 
110 static SPEW_INLINE void do_aggr PARAMS ((void));
111 static SPEW_INLINE int identifier_type PARAMS ((tree));
112 static void scan_tokens PARAMS ((int));
113 static void feed_defarg PARAMS ((tree));
114 static void finish_defarg PARAMS ((void));
115 static void yylexstring PARAMS ((struct token *));
116 static int read_token PARAMS ((struct token *));
117 
118 static SPEW_INLINE int num_tokens PARAMS ((void));
119 static SPEW_INLINE struct token *nth_token PARAMS ((int));
120 static SPEW_INLINE int next_token PARAMS ((struct token *));
121 static SPEW_INLINE int shift_token PARAMS ((void));
122 static SPEW_INLINE void push_token PARAMS ((struct token *));
123 static SPEW_INLINE void consume_token PARAMS ((void));
124 static SPEW_INLINE int read_process_identifier PARAMS ((YYSTYPE *));
125 
126 static SPEW_INLINE void feed_input PARAMS ((struct unparsed_text *));
127 static SPEW_INLINE struct token * space_for_token
128   PARAMS ((struct unparsed_text *t));
129 static SPEW_INLINE struct token * remove_last_token
130   PARAMS ((struct unparsed_text *t));
131 static struct unparsed_text * alloc_unparsed_text
132   PARAMS ((const location_t *, tree decl, int interface));
133 
134 static void snarf_block PARAMS ((struct unparsed_text *t));
135 static tree snarf_defarg PARAMS ((void));
136 static void snarf_parenthesized_expression (struct unparsed_text *);
137 static int frob_id PARAMS ((int, int, tree *));
138 
139 /* The list of inline functions being held off until we reach the end of
140    the current class declaration.  */
141 static GTY(()) struct unparsed_text *pending_inlines;
142 static GTY(()) struct unparsed_text *pending_inlines_tail;
143 
144 /* The list of previously-deferred inline functions currently being parsed.
145    This exists solely to be a GC root.  */
146 static GTY(()) struct unparsed_text *processing_these_inlines;
147 
148 static void begin_parsing_inclass_inline PARAMS ((struct unparsed_text *));
149 
150 #ifdef SPEW_DEBUG
151 int spew_debug = 0;
152 static unsigned int yylex_ctr = 0;
153 
154 static void debug_yychar PARAMS ((int));
155 
156 /* In parse.y: */
157 extern char *debug_yytranslate PARAMS ((int));
158 #endif
159 static enum cpp_ttype last_token;
160 static tree last_token_id;
161 
162 /* From lex.c: */
163 /* the declaration found for the last IDENTIFIER token read in.  yylex
164    must look this up to detect typedefs, which get token type
165    tTYPENAME, so it is left around in case the identifier is not a
166    typedef but is used in a context which makes it a reference to a
167    variable.  */
168 extern tree lastiddecl;		/* let our brains leak out here too */
169 extern int	yychar;		/*  the lookahead symbol		*/
170 extern YYSTYPE	yylval;		/*  the semantic value of the		*/
171 				/*  lookahead symbol			*/
172 /* The token fifo lives in this obstack.  */
173 static struct obstack token_obstack;
174 static int first_token;
175 
176 /* When we see a default argument in a method declaration, we snarf it as
177    text using snarf_defarg.  When we get up to namespace scope, we then go
178    through and parse all of them using do_pending_defargs.  Since yacc
179    parsers are not reentrant, we retain defargs state in these two
180    variables so that subsequent calls to do_pending_defargs can resume
181    where the previous call left off. DEFARG_FNS is a tree_list where
182    the TREE_TYPE is the current_class_type, TREE_VALUE is the FUNCTION_DECL,
183    and TREE_PURPOSE is the list unprocessed dependent functions.  */
184 
185 /* list of functions with unprocessed defargs */
186 static GTY(()) tree defarg_fns;
187 /* current default parameter */
188 static GTY(()) tree defarg_parm;
189 /* list of unprocessed fns met during current fn.  */
190 static GTY(()) tree defarg_depfns;
191 /* list of fns with circular defargs */
192 static GTY(()) tree defarg_fnsdone;
193 
194 /* Initialize obstacks. Called once, from cxx_init.  */
195 
196 void
init_spew()197 init_spew ()
198 {
199   gcc_obstack_init (&token_obstack);
200 }
201 
202 /* Subroutine of read_token.  */
203 static SPEW_INLINE int
read_process_identifier(pyylval)204 read_process_identifier (pyylval)
205      YYSTYPE *pyylval;
206 {
207   tree id = pyylval->ttype;
208 
209   if (C_IS_RESERVED_WORD (id))
210     {
211       pyylval->ttype = ridpointers[C_RID_CODE (id)];
212       return C_RID_YYCODE (id);
213     }
214 
215   /* Make sure that user does not collide with our internal naming
216      scheme.  This is not necessary if '.' is used to remove them from
217      the user's namespace, but is if '$' or double underscores are.  */
218 
219 #if !defined(JOINER) || JOINER == '$'
220   if (VPTR_NAME_P (id)
221       || VTABLE_NAME_P (id)
222       || TEMP_NAME_P (id)
223       || ANON_AGGRNAME_P (id))
224      warning (
225 "identifier name `%s' conflicts with GNU C++ internal naming strategy",
226 	      IDENTIFIER_POINTER (id));
227 #endif
228   return IDENTIFIER;
229 }
230 
231 /* Concatenate strings before returning them to the parser.  This isn't quite
232    as good as having it done in the lexer, but it's better than nothing.  */
233 
234 static void
yylexstring(t)235 yylexstring (t)
236      struct token *t;
237 {
238   enum cpp_ttype next_type;
239   tree next;
240 
241   next_type = c_lex (&next);
242   if (next_type == CPP_STRING || next_type == CPP_WSTRING)
243     {
244       varray_type strings;
245 
246       VARRAY_TREE_INIT (strings, 32, "strings");
247       VARRAY_PUSH_TREE (strings, t->yylval.ttype);
248 
249       do
250 	{
251 	  VARRAY_PUSH_TREE (strings, next);
252 	  next_type = c_lex (&next);
253 	}
254       while (next_type == CPP_STRING || next_type == CPP_WSTRING);
255 
256       t->yylval.ttype = combine_strings (strings);
257       last_token_id = t->yylval.ttype;
258     }
259 
260   /* We will have always read one token too many.  */
261   _cpp_backup_tokens (parse_in, 1);
262 
263   t->yychar = STRING;
264 }
265 
266 /* Read the next token from the input file.  The token is written into
267    T, and its type number is returned.  */
268 static int
read_token(t)269 read_token (t)
270      struct token *t;
271 {
272  retry:
273 
274   last_token = c_lex (&last_token_id);
275   t->yylval.ttype = last_token_id;
276 
277   switch (last_token)
278     {
279 #define YYCHAR(YY)	t->yychar = (YY); break;
280 #define YYCODE(C)	t->yylval.code = (C);
281 
282     case CPP_EQ:				YYCHAR('=');
283     case CPP_NOT:				YYCHAR('!');
284     case CPP_GREATER:	YYCODE(GT_EXPR);	YYCHAR('>');
285     case CPP_LESS:	YYCODE(LT_EXPR);	YYCHAR('<');
286     case CPP_PLUS:	YYCODE(PLUS_EXPR);	YYCHAR('+');
287     case CPP_MINUS:	YYCODE(MINUS_EXPR);	YYCHAR('-');
288     case CPP_MULT:	YYCODE(MULT_EXPR);	YYCHAR('*');
289     case CPP_DIV:	YYCODE(TRUNC_DIV_EXPR);	YYCHAR('/');
290     case CPP_MOD:	YYCODE(TRUNC_MOD_EXPR);	YYCHAR('%');
291     case CPP_AND:	YYCODE(BIT_AND_EXPR);	YYCHAR('&');
292     case CPP_OR:	YYCODE(BIT_IOR_EXPR);	YYCHAR('|');
293     case CPP_XOR:	YYCODE(BIT_XOR_EXPR);	YYCHAR('^');
294     case CPP_RSHIFT:	YYCODE(RSHIFT_EXPR);	YYCHAR(RSHIFT);
295     case CPP_LSHIFT:	YYCODE(LSHIFT_EXPR);	YYCHAR(LSHIFT);
296 
297     case CPP_COMPL:				YYCHAR('~');
298     case CPP_AND_AND:				YYCHAR(ANDAND);
299     case CPP_OR_OR:				YYCHAR(OROR);
300     case CPP_QUERY:				YYCHAR('?');
301     case CPP_COLON:				YYCHAR(':');
302     case CPP_COMMA:				YYCHAR(',');
303     case CPP_OPEN_PAREN:			YYCHAR('(');
304     case CPP_CLOSE_PAREN:			YYCHAR(')');
305     case CPP_EQ_EQ:	YYCODE(EQ_EXPR);	YYCHAR(EQCOMPARE);
306     case CPP_NOT_EQ:	YYCODE(NE_EXPR);	YYCHAR(EQCOMPARE);
307     case CPP_GREATER_EQ:YYCODE(GE_EXPR);	YYCHAR(ARITHCOMPARE);
308     case CPP_LESS_EQ:	YYCODE(LE_EXPR);	YYCHAR(ARITHCOMPARE);
309 
310     case CPP_PLUS_EQ:	YYCODE(PLUS_EXPR);	YYCHAR(ASSIGN);
311     case CPP_MINUS_EQ:	YYCODE(MINUS_EXPR);	YYCHAR(ASSIGN);
312     case CPP_MULT_EQ:	YYCODE(MULT_EXPR);	YYCHAR(ASSIGN);
313     case CPP_DIV_EQ:	YYCODE(TRUNC_DIV_EXPR);	YYCHAR(ASSIGN);
314     case CPP_MOD_EQ:	YYCODE(TRUNC_MOD_EXPR);	YYCHAR(ASSIGN);
315     case CPP_AND_EQ:	YYCODE(BIT_AND_EXPR);	YYCHAR(ASSIGN);
316     case CPP_OR_EQ:	YYCODE(BIT_IOR_EXPR);	YYCHAR(ASSIGN);
317     case CPP_XOR_EQ:	YYCODE(BIT_XOR_EXPR);	YYCHAR(ASSIGN);
318     case CPP_RSHIFT_EQ:	YYCODE(RSHIFT_EXPR);	YYCHAR(ASSIGN);
319     case CPP_LSHIFT_EQ:	YYCODE(LSHIFT_EXPR);	YYCHAR(ASSIGN);
320 
321     case CPP_OPEN_SQUARE:			YYCHAR('[');
322     case CPP_CLOSE_SQUARE:			YYCHAR(']');
323     case CPP_OPEN_BRACE:			YYCHAR('{');
324     case CPP_CLOSE_BRACE:			YYCHAR('}');
325     case CPP_SEMICOLON:				YYCHAR(';');
326     case CPP_ELLIPSIS:				YYCHAR(ELLIPSIS);
327 
328     case CPP_PLUS_PLUS:				YYCHAR(PLUSPLUS);
329     case CPP_MINUS_MINUS:			YYCHAR(MINUSMINUS);
330     case CPP_DEREF:				YYCHAR(POINTSAT);
331     case CPP_DOT:				YYCHAR('.');
332 
333     /* These tokens are C++ specific.  */
334     case CPP_SCOPE:				YYCHAR(SCOPE);
335     case CPP_DEREF_STAR: 			YYCHAR(POINTSAT_STAR);
336     case CPP_DOT_STAR:				YYCHAR(DOT_STAR);
337     case CPP_MIN_EQ:	YYCODE(MIN_EXPR);	YYCHAR(ASSIGN);
338     case CPP_MAX_EQ:	YYCODE(MAX_EXPR);	YYCHAR(ASSIGN);
339     case CPP_MIN:	YYCODE(MIN_EXPR);	YYCHAR(MIN_MAX);
340     case CPP_MAX:	YYCODE(MAX_EXPR);	YYCHAR(MIN_MAX);
341 #undef YYCHAR
342 #undef YYCODE
343 
344     case CPP_EOF:
345       t->yychar = 0;
346       break;
347 
348     case CPP_NAME:
349       t->yychar = read_process_identifier (&t->yylval);
350       break;
351 
352     case CPP_NUMBER:
353     case CPP_CHAR:
354     case CPP_WCHAR:
355       t->yychar = CONSTANT;
356       break;
357 
358     case CPP_STRING:
359     case CPP_WSTRING:
360       yylexstring (t);
361       break;
362 
363     default:
364       yyerror ("parse error");
365       goto retry;
366     }
367 
368   t->lineno = lineno;
369   return t->yychar;
370 }
371 
372 static void
feed_input(input)373 feed_input (input)
374      struct unparsed_text *input;
375 {
376   struct feed *f;
377 #if 0
378   if (feed)
379     abort ();
380 #endif
381 
382   f = ggc_alloc (sizeof (struct feed));
383 
384   input->cur_chunk = input->tokens;
385   input->cur_pos = 0;
386 
387 #ifdef SPEW_DEBUG
388   if (spew_debug)
389     fprintf (stderr, "\tfeeding %s:%d [%d tokens]\n",
390 	     input->locus.file, input->locus.line, input->limit - input->pos);
391 #endif
392 
393   f->input = input;
394   f->locus.file = input_filename;
395   f->locus.line = lineno;
396   f->yychar = yychar;
397   f->yylval = yylval;
398   f->first_token = first_token;
399   f->token_obstack = token_obstack;
400   f->next = feed;
401 
402   input_filename = input->locus.file;
403   lineno = input->locus.line;
404   yychar = YYEMPTY;
405   yylval.ttype = NULL_TREE;
406   first_token = 0;
407   gcc_obstack_init (&token_obstack);
408   feed = f;
409 }
410 
411 void
end_input()412 end_input ()
413 {
414   struct feed *f = feed;
415 
416   input_filename = f->locus.file;
417   lineno = f->locus.line;
418   yychar = f->yychar;
419   yylval = f->yylval;
420   first_token = f->first_token;
421   obstack_free (&token_obstack, 0);
422   token_obstack = f->token_obstack;
423   feed = f->next;
424 
425 #ifdef SPEW_DEBUG
426   if (spew_debug)
427     fprintf (stderr, "\treturning to %s:%d\n", input_filename, lineno);
428 #endif
429 }
430 
431 /* Token queue management.  */
432 
433 /* Return the number of tokens available on the fifo.  */
434 static SPEW_INLINE int
num_tokens()435 num_tokens ()
436 {
437   return (obstack_object_size (&token_obstack) / sizeof (struct token))
438     - first_token;
439 }
440 
441 /* Fetch the token N down the line from the head of the fifo.  */
442 
443 static SPEW_INLINE struct token*
nth_token(n)444 nth_token (n)
445      int n;
446 {
447 #ifdef ENABLE_CHECKING
448   /* could just have this do slurp_ implicitly, but this way is easier
449      to debug...  */
450   my_friendly_assert (n >= 0 && n < num_tokens (), 298);
451 #endif
452   return ((struct token*)obstack_base (&token_obstack)) + n + first_token;
453 }
454 
455 static const struct token Teosi = { END_OF_SAVED_INPUT, 0 UNION_INIT_ZERO };
456 static const struct token Tpad = { EMPTY, 0 UNION_INIT_ZERO };
457 
458 /* Copy the next token into T and return its value.  */
459 static SPEW_INLINE int
next_token(t)460 next_token (t)
461      struct token *t;
462 {
463   if (!feed)
464     return read_token (t);
465 
466   if (feed->input->cur_chunk != feed->input->last_chunk
467       || feed->input->cur_pos != feed->input->last_pos)
468     {
469       if (feed->input->cur_pos == TOKEN_CHUNK_SIZE)
470 	{
471 	  feed->input->cur_chunk = feed->input->cur_chunk->next;
472 	  feed->input->cur_pos = 0;
473 	}
474       memcpy (t, feed->input->cur_chunk->toks + feed->input->cur_pos,
475 	      sizeof (struct token));
476       feed->input->cur_pos++;
477       return t->yychar;
478     }
479 
480   return 0;
481 }
482 
483 /* Shift the next token onto the fifo.  */
484 static SPEW_INLINE int
shift_token()485 shift_token ()
486 {
487   size_t point = obstack_object_size (&token_obstack);
488   obstack_blank (&token_obstack, sizeof (struct token));
489   return next_token ((struct token *) (obstack_base (&token_obstack) + point));
490 }
491 
492 /* Consume the next token out of the fifo.  */
493 
494 static SPEW_INLINE void
consume_token()495 consume_token ()
496 {
497   if (num_tokens () == 1)
498     {
499       obstack_free (&token_obstack, obstack_base (&token_obstack));
500       first_token = 0;
501     }
502   else
503     first_token++;
504 }
505 
506 /* Push a token at the head of the queue; it will be the next token read.  */
507 static SPEW_INLINE void
push_token(t)508 push_token (t)
509      struct token *t;
510 {
511   if (first_token == 0)  /* We hope this doesn't happen often.  */
512     {
513       size_t active = obstack_object_size (&token_obstack);
514       obstack_blank (&token_obstack, sizeof (struct token));
515       if (active)
516 	memmove (obstack_base (&token_obstack) + sizeof (struct token),
517 		 obstack_base (&token_obstack), active);
518       first_token++;
519     }
520   first_token--;
521   memcpy (nth_token (0), t, sizeof (struct token));
522 }
523 
524 
525 /* Pull in enough tokens that the queue is N long beyond the current
526    token.  */
527 
528 static void
scan_tokens(n)529 scan_tokens (n)
530      int n;
531 {
532   int i;
533   int num = num_tokens ();
534   int yychar;
535 
536   /* First, prune any empty tokens at the end.  */
537   i = num;
538   while (i > 0 && nth_token (i - 1)->yychar == EMPTY)
539     i--;
540   if (i < num)
541     {
542       obstack_blank (&token_obstack, -((num - i) * sizeof (struct token)));
543       num = i;
544     }
545 
546   /* Now, if we already have enough tokens, return.  */
547   if (num > n)
548     return;
549 
550   /* Never read past these characters: they might separate
551      the current input stream from one we save away later.  */
552   for (i = 0; i < num; i++)
553     {
554       yychar = nth_token (i)->yychar;
555       if (yychar == '{' || yychar == ':' || yychar == ';')
556 	goto pad_tokens;
557     }
558 
559   while (num_tokens () <= n)
560     {
561       yychar = shift_token ();
562       if (yychar == '{' || yychar == ':' || yychar == ';')
563 	goto pad_tokens;
564     }
565   return;
566 
567  pad_tokens:
568   while (num_tokens () <= n)
569     obstack_grow (&token_obstack, &Tpad, sizeof (struct token));
570 }
571 
572 int looking_for_typename;
573 int looking_for_template;
574 
575 static int after_friend;
576 static int after_new;
577 static int do_snarf_defarg;
578 
579 tree got_scope;
580 tree got_object;
581 
582 static SPEW_INLINE int
identifier_type(decl)583 identifier_type (decl)
584      tree decl;
585 {
586   tree t;
587 
588   if (TREE_CODE (decl) == TEMPLATE_DECL)
589     {
590       if (TREE_CODE (DECL_TEMPLATE_RESULT (decl)) == TYPE_DECL)
591 	return PTYPENAME;
592       else if (looking_for_template)
593 	return PFUNCNAME;
594     }
595   if (looking_for_template && really_overloaded_fn (decl))
596     {
597       /* See through a baselink.  */
598       if (TREE_CODE (decl) == BASELINK)
599 	decl = BASELINK_FUNCTIONS (decl);
600 
601       for (t = decl; t != NULL_TREE; t = OVL_CHAIN (t))
602 	if (DECL_FUNCTION_TEMPLATE_P (OVL_FUNCTION (t)))
603 	  return PFUNCNAME;
604     }
605   if (TREE_CODE (decl) == NAMESPACE_DECL)
606     return NSNAME;
607   if (TREE_CODE (decl) != TYPE_DECL)
608     return IDENTIFIER;
609   if (DECL_ARTIFICIAL (decl) && TREE_TYPE (decl) == current_class_type)
610     return SELFNAME;
611 
612   /* A constructor declarator for a template type will get here as an
613      implicit typename, a TYPENAME_TYPE with a type.  */
614   t = got_scope;
615   if (t && TREE_CODE (t) == TYPENAME_TYPE)
616     t = TREE_TYPE (t);
617   decl = TREE_TYPE (decl);
618   if (TREE_CODE (decl) == TYPENAME_TYPE)
619     decl = TREE_TYPE (decl);
620   if (t && t == decl)
621     return SELFNAME;
622 
623   return tTYPENAME;
624 }
625 
626 /* token[0] == AGGR (struct/union/enum)
627    Thus, token[1] is either a tTYPENAME or a TYPENAME_DEFN.
628    If token[2] == '{' or ':' then it's TYPENAME_DEFN.
629    It's also a definition if it's a forward declaration (as in 'struct Foo;')
630    which we can tell if token[2] == ';' *and* token[-1] != FRIEND or NEW.  */
631 
632 static SPEW_INLINE void
do_aggr()633 do_aggr ()
634 {
635   int yc1, yc2;
636 
637   scan_tokens (2);
638   yc1 = nth_token (1)->yychar;
639   if (yc1 != tTYPENAME && yc1 != IDENTIFIER && yc1 != PTYPENAME)
640     return;
641   yc2 = nth_token (2)->yychar;
642   if (yc2 == ';')
643     {
644       /* It's a forward declaration iff we were not preceded by
645          'friend' or `new'.  */
646       if (after_friend || after_new)
647 	return;
648     }
649   else if (yc2 != '{' && yc2 != ':')
650     return;
651 
652   switch (yc1)
653     {
654     case tTYPENAME:
655       nth_token (1)->yychar = TYPENAME_DEFN;
656       break;
657     case PTYPENAME:
658       nth_token (1)->yychar = PTYPENAME_DEFN;
659       break;
660     case IDENTIFIER:
661       nth_token (1)->yychar = IDENTIFIER_DEFN;
662       break;
663     default:
664       abort ();
665     }
666 }
667 
668 void
see_typename()669 see_typename ()
670 {
671   /* Only types expected, not even namespaces.  */
672   looking_for_typename = 2;
673   if (yychar < 0)
674     if ((yychar = yylex ()) < 0) yychar = 0;
675   looking_for_typename = 0;
676   if (yychar == IDENTIFIER)
677     {
678       lastiddecl = lookup_name (yylval.ttype, -2);
679       if (lastiddecl)
680 	yychar = identifier_type (lastiddecl);
681     }
682 }
683 
684 int
yylex()685 yylex ()
686 {
687   int yychr;
688   int old_looking_for_typename = 0;
689   int just_saw_new = 0;
690   int just_saw_friend = 0;
691 
692   timevar_push (TV_LEX);
693 
694  retry:
695 #ifdef SPEW_DEBUG
696   if (spew_debug)
697   {
698     yylex_ctr ++;
699     fprintf (stderr, "\t\t## %d @%d ", yylex_ctr, lineno);
700   }
701 #endif
702 
703   if (do_snarf_defarg)
704     {
705       do_snarf_defarg = 0;
706       yylval.ttype = snarf_defarg ();
707       yychar = DEFARG;
708       got_object = NULL_TREE;
709       timevar_pop (TV_LEX);
710       return DEFARG;
711     }
712 
713   /* if we've got tokens, send them */
714   else if (num_tokens ())
715     yychr = nth_token (0)->yychar;
716   else
717     yychr = shift_token ();
718 
719   /* many tokens just need to be returned. At first glance, all we
720      have to do is send them back up, but some of them are needed to
721      figure out local context.  */
722   switch (yychr)
723     {
724     case EMPTY:
725       /* This is a lexical no-op.  */
726 #ifdef SPEW_DEBUG
727       if (spew_debug)
728 	debug_yychar (yychr);
729 #endif
730       consume_token ();
731       goto retry;
732 
733     case '(':
734       scan_tokens (1);
735       if (nth_token (1)->yychar == ')')
736 	{
737 	  consume_token ();
738 	  yychr = LEFT_RIGHT;
739 	}
740       break;
741 
742     case IDENTIFIER:
743     {
744       int peek;
745 
746       scan_tokens (1);
747       peek = nth_token (1)->yychar;
748       yychr = frob_id (yychr, peek, &nth_token (0)->yylval.ttype);
749       break;
750     }
751     case IDENTIFIER_DEFN:
752     case tTYPENAME:
753     case TYPENAME_DEFN:
754     case PTYPENAME:
755     case PTYPENAME_DEFN:
756       /* If we see a SCOPE next, restore the old value.
757 	 Otherwise, we got what we want.  */
758       looking_for_typename = old_looking_for_typename;
759       looking_for_template = 0;
760       break;
761 
762     case SCSPEC:
763       if (nth_token (0)->yylval.ttype == ridpointers[RID_EXTERN])
764 	{
765 	  scan_tokens (1);
766 	  if (nth_token (1)->yychar == STRING)
767 	    {
768 	      yychr = EXTERN_LANG_STRING;
769 	      nth_token (1)->yylval.ttype = get_identifier
770 		(TREE_STRING_POINTER (nth_token (1)->yylval.ttype));
771 	      consume_token ();
772 	    }
773 	}
774       /* do_aggr needs to know if the previous token was `friend'.  */
775       else if (nth_token (0)->yylval.ttype == ridpointers[RID_FRIEND])
776 	just_saw_friend = 1;
777 
778       break;
779 
780     case NEW:
781       /* do_aggr needs to know if the previous token was `new'.  */
782       just_saw_new = 1;
783       break;
784 
785     case TYPESPEC:
786     case '{':
787     case ':':
788     case ';':
789       /* If this provides a type for us, then revert lexical
790 	 state to standard state.  */
791       looking_for_typename = 0;
792       break;
793 
794     case AGGR:
795       do_aggr ();
796       break;
797 
798     case ENUM:
799       /* Set this again, in case we are rescanning.  */
800       looking_for_typename = 2;
801       break;
802 
803     default:
804       break;
805     }
806 
807   after_friend = just_saw_friend;
808   after_new = just_saw_new;
809 
810   /* class member lookup only applies to the first token after the object
811      expression, except for explicit destructor calls.  */
812   if (yychr != '~')
813     got_object = NULL_TREE;
814 
815   yychar = yychr;
816   {
817     struct token *tok = nth_token (0);
818 
819     yylval = tok->yylval;
820     if (tok->lineno)
821       lineno = tok->lineno;
822   }
823 
824 #ifdef SPEW_DEBUG
825   if (spew_debug)
826     debug_yychar (yychr);
827 #endif
828   consume_token ();
829 
830   timevar_pop (TV_LEX);
831   return yychr;
832 }
833 
834 /* Unget character CH from the input stream.
835    If RESCAN is nonzero, then we want to `see' this
836    character as the next input token.  */
837 
838 void
yyungetc(ch,rescan)839 yyungetc (ch, rescan)
840      int ch;
841      int rescan;
842 {
843   /* Unget a character from the input stream.  */
844   if (yychar == YYEMPTY || rescan == 0)
845     {
846       struct token fake;
847 
848       fake.yychar = ch;
849       fake.yylval.ttype = 0;
850       fake.lineno = lineno;
851 
852       push_token (&fake);
853     }
854   else
855     {
856       yychar = ch;
857     }
858 }
859 
860 /* Lexer hackery to determine what *IDP really is.  */
861 
862 static int
frob_id(yyc,peek,idp)863 frob_id (yyc, peek, idp)
864      int yyc;
865      int peek;
866      tree *idp;
867 {
868   tree trrr;
869   int old_looking_for_typename = 0;
870 
871   if (peek == SCOPE)
872     {
873       /* Don't interfere with the setting from an 'aggr' prefix.  */
874       old_looking_for_typename = looking_for_typename;
875       looking_for_typename = 1;
876     }
877   else if (peek == '<')
878     looking_for_template = 1;
879   trrr = lookup_name (*idp, -2);
880   if (trrr)
881     {
882       yyc = identifier_type (trrr);
883       switch(yyc)
884         {
885           case tTYPENAME:
886           case SELFNAME:
887           case NSNAME:
888           case PTYPENAME:
889 	    /* If this got special lookup, remember it.  In these
890 	       cases, we know it can't be a declarator-id.  */
891             if (got_scope || got_object)
892               *idp = trrr;
893             /* FALLTHROUGH */
894           case PFUNCNAME:
895           case IDENTIFIER:
896             lastiddecl = trrr;
897             break;
898           default:
899             abort ();
900         }
901     }
902   else
903     lastiddecl = NULL_TREE;
904   got_scope = NULL_TREE;
905   looking_for_typename = old_looking_for_typename;
906   looking_for_template = 0;
907   return yyc;
908 }
909 
910 /* ID is an operator name. Duplicate the hackery in yylex to determine what
911    it really is.  */
912 
frob_opname(id)913 tree frob_opname (id)
914      tree id;
915 {
916   scan_tokens (0);
917   frob_id (0, nth_token (0)->yychar, &id);
918   got_object = NULL_TREE;
919   return id;
920 }
921 
922 /* Set up the state required to correctly handle the definition of the
923    inline function whose preparsed state has been saved in PI.  */
924 
925 static void
begin_parsing_inclass_inline(pi)926 begin_parsing_inclass_inline (pi)
927      struct unparsed_text *pi;
928 {
929   tree context;
930 
931   /* Record that we are processing the chain of inlines starting at
932      PI for GC.  */
933   if (cfun)
934     cp_function_chain->unparsed_inlines = pi;
935   else
936     processing_these_inlines = pi;
937 
938   ggc_collect ();
939 
940   /* If this is an inline function in a local class, we must make sure
941      that we save all pertinent information about the function
942      surrounding the local class.  */
943   context = decl_function_context (pi->decl);
944   if (context)
945     push_function_context_to (context);
946 
947   feed_input (pi);
948   interface_unknown = pi->interface == 1;
949   interface_only  = pi->interface == 0;
950   DECL_PENDING_INLINE_P (pi->decl) = 0;
951   DECL_PENDING_INLINE_INFO (pi->decl) = 0;
952 
953   /* Pass back a handle to the rest of the inline functions, so that they
954      can be processed later.  */
955   yychar = PRE_PARSED_FUNCTION_DECL;
956   yylval.pi = pi;
957 
958   start_function (NULL_TREE, pi->decl, NULL_TREE,
959 		  (SF_DEFAULT | SF_PRE_PARSED | SF_INCLASS_INLINE));
960 }
961 
962 /* Called from the top level: if there are any pending inlines to
963    do, set up to process them now.  This function sets up the first function
964    to be parsed; after it has been, the rule for fndef in parse.y will
965    call process_next_inline to start working on the next one.  */
966 
967 void
do_pending_inlines()968 do_pending_inlines ()
969 {
970   /* Oops, we're still dealing with the last batch.  */
971   if (yychar == PRE_PARSED_FUNCTION_DECL)
972     return;
973 
974   if (pending_inlines)
975     {
976       /* Clear the chain, so that any inlines nested inside the batch
977 	 we're to process now don't refer to this batch.  See e.g.
978 	 g++.other/lookup6.C.  */
979       struct unparsed_text *first = pending_inlines;
980       pending_inlines = pending_inlines_tail = 0;
981 
982       begin_parsing_inclass_inline (first);
983     }
984 }
985 
986 /* Called from the fndecl rule in the parser when the function just parsed
987    was declared using a PRE_PARSED_FUNCTION_DECL (i.e. came from
988    do_pending_inlines).  */
989 
990 void
process_next_inline(i)991 process_next_inline (i)
992      struct unparsed_text *i;
993 {
994   tree decl = i->decl;
995   tree context = decl_function_context (decl);
996 
997   if (context)
998     pop_function_context_from (context);
999   if (yychar == YYEMPTY)
1000     yychar = yylex ();
1001   if (yychar != END_OF_SAVED_INPUT)
1002     error ("parse error at end of saved function text");
1003   end_input ();
1004 
1005   i = i->next;
1006   if (i)
1007     begin_parsing_inclass_inline (i);
1008   else
1009     {
1010       if (cfun)
1011 	cp_function_chain->unparsed_inlines = 0;
1012       else
1013 	processing_these_inlines = 0;
1014       extract_interface_info ();
1015     }
1016 }
1017 
1018 /* Create a new token at the end of the token list in T.  */
1019 static SPEW_INLINE struct token *
space_for_token(t)1020 space_for_token (t)
1021      struct unparsed_text *t;
1022 {
1023   if (t->last_pos != TOKEN_CHUNK_SIZE)
1024     return t->last_chunk->toks + (t->last_pos++);
1025 
1026   t->last_chunk->next = ggc_alloc_cleared (sizeof (*t->last_chunk->next));
1027   t->last_chunk = t->last_chunk->next;
1028   t->last_chunk->next = NULL;
1029 
1030   t->last_pos = 1;
1031   return t->last_chunk->toks;
1032 }
1033 
1034 /* Shrink the token list in T by one token.  */
1035 static SPEW_INLINE struct token *
remove_last_token(t)1036 remove_last_token (t)
1037      struct unparsed_text *t;
1038 {
1039   struct token *result = t->last_chunk->toks + t->last_pos - 1;
1040   if (t->last_pos == 0)
1041     abort ();
1042   t->last_pos--;
1043   if (t->last_pos == 0 && t->last_chunk != t->tokens)
1044     {
1045       struct token_chunk *c;
1046       c = t->tokens;
1047       while (c->next != t->last_chunk)
1048 	c = c->next;
1049       c->next = NULL;
1050       t->last_chunk = c;
1051       t->last_pos = ARRAY_SIZE (c->toks);
1052     }
1053   return result;
1054 }
1055 
1056 /* Allocate an 'unparsed_text' structure, ready to use space_for_token.  */
1057 static struct unparsed_text *
alloc_unparsed_text(locus,decl,interface)1058 alloc_unparsed_text (locus, decl, interface)
1059      const location_t *locus;
1060      tree decl;
1061      int interface;
1062 {
1063   struct unparsed_text *r;
1064   r = ggc_alloc_cleared (sizeof (*r));
1065   r->decl = decl;
1066   r->locus = *locus;
1067   r->interface = interface;
1068   r->tokens = r->last_chunk = ggc_alloc_cleared (sizeof (*r->tokens));
1069   return r;
1070 }
1071 
1072 /* Accumulate the tokens that make up a parenthesized expression in T,
1073    having already read the opening parenthesis.  */
1074 
1075 static void
snarf_parenthesized_expression(struct unparsed_text * t)1076 snarf_parenthesized_expression (struct unparsed_text *t)
1077 {
1078   int yyc;
1079   int level = 1;
1080 
1081   while (1)
1082     {
1083       yyc = next_token (space_for_token (t));
1084       if (yyc == '(')
1085 	++level;
1086       else if (yyc == ')' && --level == 0)
1087 	break;
1088       else if (yyc == 0)
1089 	{
1090 	  error ("%Hend of file read inside definition", &t->locus);
1091 	  break;
1092 	}
1093     }
1094 }
1095 
1096 /* Subroutine of snarf_method, deals with actual absorption of the block.  */
1097 
1098 static void
snarf_block(t)1099 snarf_block (t)
1100      struct unparsed_text *t;
1101 {
1102   int blev = 1;
1103   int look_for_semicolon = 0;
1104   int look_for_lbrac = 0;
1105   int look_for_catch = 0;
1106   int yyc;
1107   struct token *current;
1108 
1109   if (yychar == '{')
1110     ;
1111   else if (yychar == '=')
1112     look_for_semicolon = 1;
1113   else if (yychar == ':' || yychar == RETURN_KEYWORD || yychar == TRY)
1114     {
1115       if (yychar == TRY)
1116 	look_for_catch = 1;
1117       look_for_lbrac = 1;
1118       blev = 0;
1119     }
1120   else
1121     yyerror ("parse error in method specification");
1122 
1123   /* The current token is the first one to be recorded.  */
1124   current = space_for_token (t);
1125   current->yychar = yychar;
1126   current->yylval = yylval;
1127   current->lineno = lineno;
1128 
1129   for (;;)
1130     {
1131       yyc = next_token (space_for_token (t));
1132 
1133       if (yyc == '{')
1134 	{
1135 	  look_for_lbrac = 0;
1136 	  blev++;
1137 	}
1138       else if (yyc == '}')
1139 	{
1140 	  blev--;
1141 	  if (blev == 0 && !look_for_semicolon)
1142 	    {
1143 	      if (!look_for_catch)
1144 		break;
1145 
1146 	      if (next_token (space_for_token (t)) != CATCH)
1147 		{
1148 		  push_token (remove_last_token (t));
1149 		  break;
1150 		}
1151 
1152 	      look_for_lbrac = 1;
1153 	    }
1154 	}
1155       else if (yyc == ';')
1156 	{
1157 	  if (look_for_lbrac)
1158 	    {
1159 	      struct token *fake;
1160 
1161 	      error ("function body for constructor missing");
1162 	      /* fake a { } to avoid further errors */
1163 	      fake = space_for_token (t);
1164 	      fake->yylval.ttype = 0;
1165 	      fake->yychar = '{';
1166 	      fake = space_for_token (t);
1167 	      fake->yylval.ttype = 0;
1168 	      fake->yychar = '}';
1169 	      break;
1170 	    }
1171 	  else if (look_for_semicolon && blev == 0)
1172 	    break;
1173 	}
1174       else if (yyc == '(' && blev == 0)
1175 	snarf_parenthesized_expression (t);
1176       else if (yyc == 0)
1177 	{
1178 	  error ("%Hend of file read inside definition", &t->locus);
1179 	  break;
1180 	}
1181     }
1182 }
1183 
1184 /* This function stores away the text for an inline function that should
1185    be processed later (by do_pending_inlines).  */
1186 void
snarf_method(decl)1187 snarf_method (decl)
1188      tree decl;
1189 {
1190   struct unparsed_text *meth;
1191   location_t starting;
1192   starting.file = input_filename;
1193   starting.line = lineno;
1194 
1195   meth = alloc_unparsed_text (&starting, decl, (interface_unknown ? 1
1196 						: (interface_only ? 0 : 2)));
1197 
1198   snarf_block (meth);
1199   /* Add three END_OF_SAVED_INPUT tokens.  We used to provide an
1200      infinite stream of END_OF_SAVED_INPUT tokens -- but that can
1201      cause the compiler to get stuck in an infinite loop when
1202      encountering invalid code.  We need more than one because the
1203      parser sometimes peeks ahead several tokens.  */
1204   memcpy (space_for_token (meth), &Teosi, sizeof (struct token));
1205   memcpy (space_for_token (meth), &Teosi, sizeof (struct token));
1206   memcpy (space_for_token (meth), &Teosi, sizeof (struct token));
1207 
1208   /* Happens when we get two declarations of the same function in the
1209      same scope.  */
1210   if (decl == void_type_node
1211       || (current_class_type && TYPE_REDEFINED (current_class_type)))
1212     return;
1213 
1214 #ifdef SPEW_DEBUG
1215   if (spew_debug)
1216     fprintf (stderr, "\tsaved method of %d tokens from %s:%d\n",
1217 	     meth->limit, starting.file, starting.line);
1218 #endif
1219 
1220   DECL_PENDING_INLINE_INFO (decl) = meth;
1221   DECL_PENDING_INLINE_P (decl) = 1;
1222 
1223   /* We need to know that this was defined in the class, so that
1224      friend templates are handled correctly.  */
1225   DECL_INITIALIZED_IN_CLASS_P (decl) = 1;
1226 
1227   if (pending_inlines_tail)
1228     pending_inlines_tail->next = meth;
1229   else
1230     pending_inlines = meth;
1231   pending_inlines_tail = meth;
1232 }
1233 
1234 /* Consume a no-commas expression - a default argument - and return
1235    a DEFAULT_ARG tree node.  */
1236 
1237 static tree
snarf_defarg()1238 snarf_defarg ()
1239 {
1240   int yyc;
1241   int plev = 0;
1242   struct unparsed_text *buf;
1243   tree arg;
1244   location_t starting;
1245   starting.file = input_filename;
1246   starting.line = lineno;
1247 
1248   buf = alloc_unparsed_text (&starting, 0, 0);
1249 
1250   for (;;)
1251     {
1252       yyc = next_token (space_for_token (buf));
1253 
1254       if (plev <= 0 && (yyc == ')' || yyc == ','))
1255 	break;
1256       else if (yyc == '(' || yyc == '[')
1257 	++plev;
1258       else if (yyc == ']' || yyc == ')')
1259 	--plev;
1260       else if (yyc == 0)
1261 	{
1262 	  error ("%Hend of file read inside default argument", &starting);
1263 	  goto done;
1264 	}
1265     }
1266 
1267   /* Unget the last token.  */
1268   push_token (remove_last_token (buf));
1269   /* Add three END_OF_SAVED_INPUT tokens.  We used to provide an
1270      infinite stream of END_OF_SAVED_INPUT tokens -- but that can
1271      cause the compiler to get stuck in an infinite loop when
1272      encountering invalid code.  We need more than one because the
1273      parser sometimes peeks ahead several tokens.  */
1274   memcpy (space_for_token (buf), &Teosi, sizeof (struct token));
1275   memcpy (space_for_token (buf), &Teosi, sizeof (struct token));
1276   memcpy (space_for_token (buf), &Teosi, sizeof (struct token));
1277 
1278  done:
1279 #ifdef SPEW_DEBUG
1280   if (spew_debug)
1281     fprintf (stderr, "\tsaved defarg of %d tokens from %s:%d\n",
1282 	     buf->limit, starting.file, starting.line);
1283 #endif
1284 
1285   arg = make_node (DEFAULT_ARG);
1286   DEFARG_POINTER (arg) = (char *)buf;
1287 
1288   return arg;
1289 }
1290 
1291 /* Decide whether the default argument we are about to see should be
1292    gobbled up as text for later parsing.  */
1293 
1294 void
maybe_snarf_defarg()1295 maybe_snarf_defarg ()
1296 {
1297   if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
1298     do_snarf_defarg = 1;
1299 }
1300 
1301 /* Called from grokfndecl to note a function decl with unparsed default
1302    arguments for later processing.  Also called from grokdeclarator
1303    for function types with unparsed defargs; the call from grokfndecl
1304    will always come second, so we can overwrite the entry from the type.  */
1305 
1306 void
add_defarg_fn(decl)1307 add_defarg_fn (decl)
1308      tree decl;
1309 {
1310   if (TREE_CODE (decl) == FUNCTION_DECL)
1311     TREE_VALUE (defarg_fns) = decl;
1312   else
1313     {
1314       defarg_fns = tree_cons (NULL_TREE, decl, defarg_fns);
1315       TREE_TYPE (defarg_fns) = current_class_type;
1316     }
1317 }
1318 
1319 /* Helper for do_pending_defargs.  Starts the parsing of a default arg.  */
1320 
1321 static void
feed_defarg(p)1322 feed_defarg (p)
1323      tree p;
1324 {
1325   tree d = TREE_PURPOSE (p);
1326 
1327   feed_input ((struct unparsed_text *)DEFARG_POINTER (d));
1328   yychar = DEFARG_MARKER;
1329   yylval.ttype = p;
1330 }
1331 
1332 /* Helper for do_pending_defargs.  Ends the parsing of a default arg.  */
1333 
1334 static void
finish_defarg()1335 finish_defarg ()
1336 {
1337   if (yychar == YYEMPTY)
1338     yychar = yylex ();
1339   if (yychar != END_OF_SAVED_INPUT)
1340     error ("parse error at end of saved function text");
1341 
1342   end_input ();
1343 }
1344 
1345 /* Main function for deferred parsing of default arguments.  Called from
1346    the parser.  */
1347 
1348 void
do_pending_defargs()1349 do_pending_defargs ()
1350 {
1351   if (defarg_parm)
1352     finish_defarg ();
1353 
1354   for (; defarg_fns;)
1355     {
1356       tree current = defarg_fns;
1357 
1358       tree defarg_fn = TREE_VALUE (defarg_fns);
1359       if (defarg_parm == NULL_TREE)
1360 	{
1361 	  push_nested_class (TREE_TYPE (defarg_fns), 1);
1362 	  pushlevel (0);
1363 	  if (TREE_CODE (defarg_fn) == FUNCTION_DECL)
1364 	    maybe_begin_member_template_processing (defarg_fn);
1365 
1366 	  if (TREE_CODE (defarg_fn) == FUNCTION_DECL)
1367 	    defarg_parm = TYPE_ARG_TYPES (TREE_TYPE (defarg_fn));
1368 	  else
1369 	    defarg_parm = TYPE_ARG_TYPES (defarg_fn);
1370 	}
1371       else
1372 	defarg_parm = TREE_CHAIN (defarg_parm);
1373 
1374       for (; defarg_parm; defarg_parm = TREE_CHAIN (defarg_parm))
1375 	if (!TREE_PURPOSE (defarg_parm)
1376 	    || TREE_CODE (TREE_PURPOSE (defarg_parm)) != DEFAULT_ARG)
1377 	  ;/* OK */
1378 	else if (TREE_PURPOSE (current) == error_mark_node)
1379 	  DEFARG_POINTER (TREE_PURPOSE (defarg_parm)) = NULL;
1380 	else
1381 	  {
1382 	    feed_defarg (defarg_parm);
1383 
1384 	    /* Return to the parser, which will process this defarg
1385 	       and call us again.  */
1386 	    return;
1387 	  }
1388 
1389       if (TREE_CODE (defarg_fn) == FUNCTION_DECL)
1390 	{
1391 	  maybe_end_member_template_processing ();
1392 	  check_default_args (defarg_fn);
1393 	}
1394 
1395       poplevel (0, 0, 0);
1396       pop_nested_class ();
1397 
1398       defarg_fns = TREE_CHAIN (defarg_fns);
1399       if (defarg_depfns)
1400         {
1401           /* This function's default args depend on unprocessed default args
1402              of defarg_fns. We will need to reprocess this function, and
1403              check for circular dependencies.  */
1404           tree a, b;
1405 
1406           for (a = defarg_depfns, b = TREE_PURPOSE (current); a && b;
1407                a = TREE_CHAIN (a), b = TREE_CHAIN (b))
1408             if (TREE_VALUE (a) != TREE_VALUE (b))
1409               goto different;
1410           if (a || b)
1411             {
1412             different:;
1413               TREE_CHAIN (current) = NULL_TREE;
1414               defarg_fns = chainon (defarg_fns, current);
1415               TREE_PURPOSE (current) = defarg_depfns;
1416             }
1417           else
1418             {
1419               cp_warning_at ("circular dependency in default args of `%#D'", defarg_fn);
1420               /* No need to say what else is dependent, as they will be
1421                  picked up in another pass.  */
1422 
1423               /* Immediately repeat, but marked so that we break the loop.  */
1424               defarg_fns = current;
1425               TREE_PURPOSE (current) = error_mark_node;
1426             }
1427           defarg_depfns = NULL_TREE;
1428         }
1429       else if (TREE_PURPOSE (current) == error_mark_node)
1430         defarg_fnsdone = tree_cons (NULL_TREE, defarg_fn, defarg_fnsdone);
1431     }
1432 }
1433 
1434 /* After parsing all the default arguments, we must clear any that remain,
1435    which will be part of a circular dependency.  */
1436 void
done_pending_defargs()1437 done_pending_defargs ()
1438 {
1439   for (; defarg_fnsdone; defarg_fnsdone = TREE_CHAIN (defarg_fnsdone))
1440     {
1441       tree fn = TREE_VALUE (defarg_fnsdone);
1442       tree parms;
1443 
1444       if (TREE_CODE (fn) == FUNCTION_DECL)
1445         parms = TYPE_ARG_TYPES (TREE_TYPE (fn));
1446       else
1447         parms = TYPE_ARG_TYPES (fn);
1448       for (; parms; parms = TREE_CHAIN (parms))
1449 	if (TREE_PURPOSE (parms)
1450 	    && TREE_CODE (TREE_PURPOSE (parms)) == DEFAULT_ARG)
1451 	  {
1452             my_friendly_assert (!DEFARG_POINTER (TREE_PURPOSE (parms)), 20010107);
1453 	    TREE_PURPOSE (parms) = NULL_TREE;
1454 	  }
1455     }
1456 }
1457 
1458 /* In processing the current default arg, we called FN, but that call
1459    required a default argument of FN, and that had not yet been processed.
1460    Remember FN.  */
1461 
1462 void
unprocessed_defarg_fn(fn)1463 unprocessed_defarg_fn (fn)
1464      tree fn;
1465 {
1466   defarg_depfns = tree_cons (NULL_TREE, fn, defarg_depfns);
1467 }
1468 
1469 /* Called from the parser to update an element of TYPE_ARG_TYPES for some
1470    FUNCTION_TYPE with the newly parsed version of its default argument, which
1471    was previously digested as text.  */
1472 
1473 void
replace_defarg(arg,init)1474 replace_defarg (arg, init)
1475      tree arg, init;
1476 {
1477   if (init == error_mark_node)
1478     TREE_PURPOSE (arg) = error_mark_node;
1479   else
1480     {
1481       if (! processing_template_decl
1482           && ! can_convert_arg (TREE_VALUE (arg), TREE_TYPE (init), init))
1483         pedwarn ("invalid type `%T' for default argument to `%T'",
1484   	    	    TREE_TYPE (init), TREE_VALUE (arg));
1485       if (!defarg_depfns)
1486         TREE_PURPOSE (arg) = init;
1487     }
1488 }
1489 
1490 #ifdef SPEW_DEBUG
1491 /* debug_yychar takes a yychar (token number) value and prints its name.  */
1492 
1493 static void
debug_yychar(yy)1494 debug_yychar (yy)
1495      int yy;
1496 {
1497   if (yy<256)
1498     fprintf (stderr, "->%d < %c >\n", lineno, yy);
1499   else if (yy == IDENTIFIER || yy == tTYPENAME)
1500     {
1501       const char *id;
1502       if (TREE_CODE (yylval.ttype) == IDENTIFIER_NODE)
1503 	id = IDENTIFIER_POINTER (yylval.ttype);
1504       else if (TREE_CODE_CLASS (TREE_CODE (yylval.ttype)) == 'd')
1505 	id = IDENTIFIER_POINTER (DECL_NAME (yylval.ttype));
1506       else
1507 	id = "";
1508       fprintf (stderr, "->%d <%s `%s'>\n", lineno, debug_yytranslate (yy), id);
1509     }
1510   else
1511     fprintf (stderr, "->%d <%s>\n", lineno, debug_yytranslate (yy));
1512 }
1513 
1514 #endif
1515 
1516 #define NAME(TYPE) cpp_type2name (TYPE)
1517 
1518 void
yyerror(msgid)1519 yyerror (msgid)
1520      const char *msgid;
1521 {
1522   const char *string = _(msgid);
1523 
1524   if (last_token == CPP_EOF)
1525     error ("%s at end of input", string);
1526   else if (last_token == CPP_CHAR || last_token == CPP_WCHAR)
1527     {
1528       if (yylval.ttype && TREE_CODE (yylval.ttype) == INTEGER_CST)
1529 	{
1530 	  unsigned int val = TREE_INT_CST_LOW (yylval.ttype);
1531 	  const char *const ell = (last_token == CPP_CHAR) ? "" : "L";
1532 	  if (val <= UCHAR_MAX && ISGRAPH (val))
1533 	    error ("%s before %s'%c'", string, ell, val);
1534 	  else
1535 	    error ("%s before %s'\\x%x'", string, ell, val);
1536 	}
1537       else
1538 	error ("%s", string);
1539     }
1540   else if (last_token == CPP_STRING
1541 	   || last_token == CPP_WSTRING)
1542     error ("%s before string constant", string);
1543   else if (last_token == CPP_NUMBER)
1544     error ("%s before numeric constant", string);
1545   else if (last_token == CPP_NAME)
1546     {
1547       if (TREE_CODE (last_token_id) == IDENTIFIER_NODE)
1548         error ("%s before `%s'", string, IDENTIFIER_POINTER (last_token_id));
1549       else if (ISGRAPH (yychar))
1550         error ("%s before `%c'", string, yychar);
1551       else
1552 	error ("%s before `\%o'", string, yychar);
1553     }
1554   else
1555     error ("%s before `%s' token", string, NAME (last_token));
1556 }
1557 
1558 #include "gt-cp-spew.h"
1559