OSDN Git Service

2002-08-24 Matt Austern <austern@apple.com>
[pf3gnuchains/gcc-fork.git] / gcc / cp / spew.c
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 int frob_id PARAMS ((int, int, tree *));
137
138 /* The list of inline functions being held off until we reach the end of
139    the current class declaration.  */
140 static GTY(()) struct unparsed_text *pending_inlines;
141 static GTY(()) struct unparsed_text *pending_inlines_tail;
142
143 /* The list of previously-deferred inline functions currently being parsed.
144    This exists solely to be a GC root.  */
145 static GTY(()) struct unparsed_text *processing_these_inlines;
146
147 static void begin_parsing_inclass_inline PARAMS ((struct unparsed_text *));
148
149 #ifdef SPEW_DEBUG
150 int spew_debug = 0;
151 static unsigned int yylex_ctr = 0;
152
153 static void debug_yychar PARAMS ((int));
154
155 /* In parse.y: */
156 extern char *debug_yytranslate PARAMS ((int));
157 #endif
158 static enum cpp_ttype last_token;
159 static tree last_token_id;
160
161 /* From lex.c: */
162 /* the declaration found for the last IDENTIFIER token read in.  yylex
163    must look this up to detect typedefs, which get token type
164    tTYPENAME, so it is left around in case the identifier is not a
165    typedef but is used in a context which makes it a reference to a
166    variable.  */
167 extern tree lastiddecl;         /* let our brains leak out here too */
168 extern int      yychar;         /*  the lookahead symbol                */
169 extern YYSTYPE  yylval;         /*  the semantic value of the           */
170                                 /*  lookahead symbol                    */
171 /* The token fifo lives in this obstack.  */
172 static struct obstack token_obstack;
173 static int first_token;
174
175 /* When we see a default argument in a method declaration, we snarf it as
176    text using snarf_defarg.  When we get up to namespace scope, we then go
177    through and parse all of them using do_pending_defargs.  Since yacc
178    parsers are not reentrant, we retain defargs state in these two
179    variables so that subsequent calls to do_pending_defargs can resume
180    where the previous call left off. DEFARG_FNS is a tree_list where
181    the TREE_TYPE is the current_class_type, TREE_VALUE is the FUNCTION_DECL,
182    and TREE_PURPOSE is the list unprocessed dependent functions.  */
183
184 /* list of functions with unprocessed defargs */
185 static GTY(()) tree defarg_fns;
186 /* current default parameter */
187 static GTY(()) tree defarg_parm;
188 /* list of unprocessed fns met during current fn. */
189 static GTY(()) tree defarg_depfns;
190 /* list of fns with circular defargs */
191 static GTY(()) tree defarg_fnsdone;
192
193 /* Initialize obstacks. Called once, from cxx_init.  */
194
195 void
196 init_spew ()
197 {
198   gcc_obstack_init (&token_obstack);
199 }
200
201 /* Subroutine of read_token.  */
202 static SPEW_INLINE int
203 read_process_identifier (pyylval)
204      YYSTYPE *pyylval;
205 {
206   tree id = pyylval->ttype;
207
208   if (C_IS_RESERVED_WORD (id))
209     {
210       pyylval->ttype = ridpointers[C_RID_CODE (id)];
211       return C_RID_YYCODE (id);
212     }
213
214   /* Make sure that user does not collide with our internal naming
215      scheme.  This is not necessary if '.' is used to remove them from
216      the user's namespace, but is if '$' or double underscores are.  */
217
218 #if !defined(JOINER) || JOINER == '$'
219   if (VPTR_NAME_P (id)
220       || VTABLE_NAME_P (id)
221       || TEMP_NAME_P (id)
222       || ANON_AGGRNAME_P (id))
223      warning (
224 "identifier name `%s' conflicts with GNU C++ internal naming strategy",
225               IDENTIFIER_POINTER (id));
226 #endif
227   return IDENTIFIER;
228 }
229
230 /* Concatenate strings before returning them to the parser.  This isn't quite
231    as good as having it done in the lexer, but it's better than nothing.  */
232
233 static void
234 yylexstring (t)
235      struct token *t;
236 {
237   enum cpp_ttype next_type;
238   tree next;
239
240   next_type = c_lex (&next);
241   if (next_type == CPP_STRING || next_type == CPP_WSTRING)
242     {
243       varray_type strings;
244
245       VARRAY_TREE_INIT (strings, 32, "strings");
246       VARRAY_PUSH_TREE (strings, t->yylval.ttype);
247
248       do
249         {
250           VARRAY_PUSH_TREE (strings, next);
251           next_type = c_lex (&next);
252         }
253       while (next_type == CPP_STRING || next_type == CPP_WSTRING);
254
255       t->yylval.ttype = combine_strings (strings);
256       last_token_id = t->yylval.ttype;
257     }
258
259   /* We will have always read one token too many.  */
260   _cpp_backup_tokens (parse_in, 1);
261
262   t->yychar = STRING;
263 }
264
265 /* Read the next token from the input file.  The token is written into
266    T, and its type number is returned.  */
267 static int
268 read_token (t)
269      struct token *t;
270 {
271  retry:
272
273   last_token = c_lex (&last_token_id);
274   t->yylval.ttype = last_token_id;
275
276   switch (last_token)
277     {
278 #define YYCHAR(YY)      t->yychar = (YY); break;
279 #define YYCODE(C)       t->yylval.code = (C);
280
281     case CPP_EQ:                                YYCHAR('=');
282     case CPP_NOT:                               YYCHAR('!');
283     case CPP_GREATER:   YYCODE(GT_EXPR);        YYCHAR('>');
284     case CPP_LESS:      YYCODE(LT_EXPR);        YYCHAR('<');
285     case CPP_PLUS:      YYCODE(PLUS_EXPR);      YYCHAR('+');
286     case CPP_MINUS:     YYCODE(MINUS_EXPR);     YYCHAR('-');
287     case CPP_MULT:      YYCODE(MULT_EXPR);      YYCHAR('*');
288     case CPP_DIV:       YYCODE(TRUNC_DIV_EXPR); YYCHAR('/');
289     case CPP_MOD:       YYCODE(TRUNC_MOD_EXPR); YYCHAR('%');
290     case CPP_AND:       YYCODE(BIT_AND_EXPR);   YYCHAR('&');
291     case CPP_OR:        YYCODE(BIT_IOR_EXPR);   YYCHAR('|');
292     case CPP_XOR:       YYCODE(BIT_XOR_EXPR);   YYCHAR('^');
293     case CPP_RSHIFT:    YYCODE(RSHIFT_EXPR);    YYCHAR(RSHIFT);
294     case CPP_LSHIFT:    YYCODE(LSHIFT_EXPR);    YYCHAR(LSHIFT);
295
296     case CPP_COMPL:                             YYCHAR('~');
297     case CPP_AND_AND:                           YYCHAR(ANDAND);
298     case CPP_OR_OR:                             YYCHAR(OROR);
299     case CPP_QUERY:                             YYCHAR('?');
300     case CPP_COLON:                             YYCHAR(':');
301     case CPP_COMMA:                             YYCHAR(',');
302     case CPP_OPEN_PAREN:                        YYCHAR('(');
303     case CPP_CLOSE_PAREN:                       YYCHAR(')');
304     case CPP_EQ_EQ:     YYCODE(EQ_EXPR);        YYCHAR(EQCOMPARE);
305     case CPP_NOT_EQ:    YYCODE(NE_EXPR);        YYCHAR(EQCOMPARE);
306     case CPP_GREATER_EQ:YYCODE(GE_EXPR);        YYCHAR(ARITHCOMPARE);
307     case CPP_LESS_EQ:   YYCODE(LE_EXPR);        YYCHAR(ARITHCOMPARE);
308
309     case CPP_PLUS_EQ:   YYCODE(PLUS_EXPR);      YYCHAR(ASSIGN);
310     case CPP_MINUS_EQ:  YYCODE(MINUS_EXPR);     YYCHAR(ASSIGN);
311     case CPP_MULT_EQ:   YYCODE(MULT_EXPR);      YYCHAR(ASSIGN);
312     case CPP_DIV_EQ:    YYCODE(TRUNC_DIV_EXPR); YYCHAR(ASSIGN);
313     case CPP_MOD_EQ:    YYCODE(TRUNC_MOD_EXPR); YYCHAR(ASSIGN);
314     case CPP_AND_EQ:    YYCODE(BIT_AND_EXPR);   YYCHAR(ASSIGN);
315     case CPP_OR_EQ:     YYCODE(BIT_IOR_EXPR);   YYCHAR(ASSIGN);
316     case CPP_XOR_EQ:    YYCODE(BIT_XOR_EXPR);   YYCHAR(ASSIGN);
317     case CPP_RSHIFT_EQ: YYCODE(RSHIFT_EXPR);    YYCHAR(ASSIGN);
318     case CPP_LSHIFT_EQ: YYCODE(LSHIFT_EXPR);    YYCHAR(ASSIGN);
319
320     case CPP_OPEN_SQUARE:                       YYCHAR('[');
321     case CPP_CLOSE_SQUARE:                      YYCHAR(']');
322     case CPP_OPEN_BRACE:                        YYCHAR('{');
323     case CPP_CLOSE_BRACE:                       YYCHAR('}');
324     case CPP_SEMICOLON:                         YYCHAR(';');
325     case CPP_ELLIPSIS:                          YYCHAR(ELLIPSIS);
326
327     case CPP_PLUS_PLUS:                         YYCHAR(PLUSPLUS);
328     case CPP_MINUS_MINUS:                       YYCHAR(MINUSMINUS);
329     case CPP_DEREF:                             YYCHAR(POINTSAT);
330     case CPP_DOT:                               YYCHAR('.');
331
332     /* These tokens are C++ specific.  */
333     case CPP_SCOPE:                             YYCHAR(SCOPE);
334     case CPP_DEREF_STAR:                        YYCHAR(POINTSAT_STAR);
335     case CPP_DOT_STAR:                          YYCHAR(DOT_STAR);
336     case CPP_MIN_EQ:    YYCODE(MIN_EXPR);       YYCHAR(ASSIGN);
337     case CPP_MAX_EQ:    YYCODE(MAX_EXPR);       YYCHAR(ASSIGN);
338     case CPP_MIN:       YYCODE(MIN_EXPR);       YYCHAR(MIN_MAX);
339     case CPP_MAX:       YYCODE(MAX_EXPR);       YYCHAR(MIN_MAX);
340 #undef YYCHAR
341 #undef YYCODE
342
343     case CPP_EOF:
344       t->yychar = 0;
345       break;
346
347     case CPP_NAME:
348       t->yychar = read_process_identifier (&t->yylval);
349       break;
350
351     case CPP_NUMBER:
352     case CPP_CHAR:
353     case CPP_WCHAR:
354       t->yychar = CONSTANT;
355       break;
356
357     case CPP_STRING:
358     case CPP_WSTRING:
359       yylexstring (t);
360       break;
361
362     default:
363       yyerror ("parse error");
364       goto retry;
365     }
366
367   t->lineno = lineno;
368   return t->yychar;
369 }
370
371 static void
372 feed_input (input)
373      struct unparsed_text *input;
374 {
375   struct feed *f;
376 #if 0
377   if (feed)
378     abort ();
379 #endif
380
381   f = ggc_alloc (sizeof (struct feed));
382
383   input->cur_chunk = input->tokens;
384   input->cur_pos = 0;
385
386 #ifdef SPEW_DEBUG
387   if (spew_debug)
388     fprintf (stderr, "\tfeeding %s:%d [%d tokens]\n",
389              input->locus.file, input->locus.line, input->limit - input->pos);
390 #endif
391
392   f->input = input;
393   f->locus.file = input_filename;
394   f->locus.line = lineno;
395   f->yychar = yychar;
396   f->yylval = yylval;
397   f->first_token = first_token;
398   f->token_obstack = token_obstack;
399   f->next = feed;
400
401   input_filename = input->locus.file;
402   lineno = input->locus.line;
403   yychar = YYEMPTY;
404   yylval.ttype = NULL_TREE;
405   first_token = 0;
406   gcc_obstack_init (&token_obstack);
407   feed = f;
408 }
409
410 void
411 end_input ()
412 {
413   struct feed *f = feed;
414
415   input_filename = f->locus.file;
416   lineno = f->locus.line;
417   yychar = f->yychar;
418   yylval = f->yylval;
419   first_token = f->first_token;
420   obstack_free (&token_obstack, 0);
421   token_obstack = f->token_obstack;
422   feed = f->next;
423
424 #ifdef SPEW_DEBUG
425   if (spew_debug)
426     fprintf (stderr, "\treturning to %s:%d\n", input_filename, lineno);
427 #endif
428 }
429
430 /* Token queue management.  */
431
432 /* Return the number of tokens available on the fifo.  */
433 static SPEW_INLINE int
434 num_tokens ()
435 {
436   return (obstack_object_size (&token_obstack) / sizeof (struct token))
437     - first_token;
438 }
439
440 /* Fetch the token N down the line from the head of the fifo.  */
441
442 static SPEW_INLINE struct token*
443 nth_token (n)
444      int n;
445 {
446 #ifdef ENABLE_CHECKING
447   /* could just have this do slurp_ implicitly, but this way is easier
448      to debug...  */
449   my_friendly_assert (n >= 0 && n < num_tokens (), 298);
450 #endif
451   return ((struct token*)obstack_base (&token_obstack)) + n + first_token;
452 }
453
454 static const struct token Teosi = { END_OF_SAVED_INPUT, 0 UNION_INIT_ZERO };
455 static const struct token Tpad = { EMPTY, 0 UNION_INIT_ZERO };
456
457 /* Copy the next token into T and return its value.  */
458 static SPEW_INLINE int
459 next_token (t)
460      struct token *t;
461 {
462   if (!feed)
463     return read_token (t);
464
465   if (feed->input->cur_chunk != feed->input->last_chunk
466       || feed->input->cur_pos != feed->input->last_pos)
467     {
468       if (feed->input->cur_pos == TOKEN_CHUNK_SIZE)
469         {
470           feed->input->cur_chunk = feed->input->cur_chunk->next;
471           feed->input->cur_pos = 0;
472         }
473       memcpy (t, feed->input->cur_chunk->toks + feed->input->cur_pos,
474               sizeof (struct token));
475       feed->input->cur_pos++;
476       return t->yychar;
477     }
478
479   memcpy (t, &Teosi, sizeof (struct token));
480   return END_OF_SAVED_INPUT;
481 }
482
483 /* Shift the next token onto the fifo.  */
484 static SPEW_INLINE int
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
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
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
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
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
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
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
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 non-zero, then we want to `see' this
836    character as the next input token.  */
837
838 void
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
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
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
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
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
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 \f
1018 /* Create a new token at the end of the token list in T.  */
1019 static SPEW_INLINE struct token *
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 *
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 **tc;
1046       for (tc = &t->tokens; (*tc)->next != NULL; tc = &(*tc)->next)
1047         ;
1048       *tc = NULL;
1049       t->last_pos = ARRAY_SIZE ((*tc)->toks);
1050     }
1051   return result;
1052 }
1053
1054 /* Allocate an 'unparsed_text' structure, ready to use space_for_token.  */
1055 static struct unparsed_text *
1056 alloc_unparsed_text (locus, decl, interface)
1057      const location_t *locus;
1058      tree decl;
1059      int interface;
1060 {
1061   struct unparsed_text *r;
1062   r = ggc_alloc_cleared (sizeof (*r));
1063   r->decl = decl;
1064   r->locus = *locus;
1065   r->interface = interface;
1066   r->tokens = r->last_chunk = ggc_alloc_cleared (sizeof (*r->tokens));
1067   return r;
1068 }
1069
1070 /* Subroutine of snarf_method, deals with actual absorption of the block.  */
1071
1072 static void
1073 snarf_block (t)
1074      struct unparsed_text *t;
1075 {
1076   int blev = 1;
1077   int look_for_semicolon = 0;
1078   int look_for_lbrac = 0;
1079   int look_for_catch = 0;
1080   int yyc;
1081   struct token *current;
1082
1083   if (yychar == '{')
1084     ;
1085   else if (yychar == '=')
1086     look_for_semicolon = 1;
1087   else if (yychar == ':' || yychar == RETURN_KEYWORD || yychar == TRY)
1088     {
1089       if (yychar == TRY)
1090         look_for_catch = 1;
1091       look_for_lbrac = 1;
1092       blev = 0;
1093     }
1094   else
1095     yyerror ("parse error in method specification");
1096
1097   /* The current token is the first one to be recorded.  */
1098   current = space_for_token (t);
1099   current->yychar = yychar;
1100   current->yylval = yylval;
1101   current->lineno = lineno;
1102
1103   for (;;)
1104     {
1105       yyc = next_token (space_for_token (t));
1106
1107       if (yyc == '{')
1108         {
1109           look_for_lbrac = 0;
1110           blev++;
1111         }
1112       else if (yyc == '}')
1113         {
1114           blev--;
1115           if (blev == 0 && !look_for_semicolon)
1116             {
1117               if (!look_for_catch)
1118                 break;
1119
1120               if (next_token (space_for_token (t)) != CATCH)
1121                 {
1122                   push_token (remove_last_token (t));
1123                   break;
1124                 }
1125
1126               look_for_lbrac = 1;
1127             }
1128         }
1129       else if (yyc == ';')
1130         {
1131           if (look_for_lbrac)
1132             {
1133               struct token *fake;
1134
1135               error ("function body for constructor missing");
1136               /* fake a { } to avoid further errors */
1137               fake = space_for_token (t);
1138               fake->yylval.ttype = 0;
1139               fake->yychar = '{';
1140               fake = space_for_token (t);
1141               fake->yylval.ttype = 0;
1142               fake->yychar = '}';
1143               break;
1144             }
1145           else if (look_for_semicolon && blev == 0)
1146             break;
1147         }
1148       else if (yyc == 0)
1149         {
1150           error ("%Hend of file read inside definition", &t->locus);
1151           break;
1152         }
1153     }
1154 }
1155
1156 /* This function stores away the text for an inline function that should
1157    be processed later (by do_pending_inlines).  */
1158 void
1159 snarf_method (decl)
1160      tree decl;
1161 {
1162   struct unparsed_text *meth;
1163   location_t starting;
1164   starting.file = input_filename;
1165   starting.line = lineno;
1166
1167   meth = alloc_unparsed_text (&starting, decl, (interface_unknown ? 1
1168                                                 : (interface_only ? 0 : 2)));
1169
1170   snarf_block (meth);
1171
1172   /* Happens when we get two declarations of the same function in the
1173      same scope.  */
1174   if (decl == void_type_node
1175       || (current_class_type && TYPE_REDEFINED (current_class_type)))
1176     return;
1177
1178 #ifdef SPEW_DEBUG
1179   if (spew_debug)
1180     fprintf (stderr, "\tsaved method of %d tokens from %s:%d\n",
1181              meth->limit, starting.file, starting.line);
1182 #endif
1183
1184   DECL_PENDING_INLINE_INFO (decl) = meth;
1185   DECL_PENDING_INLINE_P (decl) = 1;
1186
1187   if (pending_inlines_tail)
1188     pending_inlines_tail->next = meth;
1189   else
1190     pending_inlines = meth;
1191   pending_inlines_tail = meth;
1192 }
1193
1194 /* Consume a no-commas expression - a default argument - and return
1195    a DEFAULT_ARG tree node.  */
1196
1197 static tree
1198 snarf_defarg ()
1199 {
1200   int yyc;
1201   int plev = 0;
1202   struct unparsed_text *buf;
1203   tree arg;
1204   location_t starting;
1205   starting.file = input_filename;
1206   starting.line = lineno;
1207
1208   buf = alloc_unparsed_text (&starting, 0, 0);
1209
1210   for (;;)
1211     {
1212       yyc = next_token (space_for_token (buf));
1213
1214       if (plev <= 0 && (yyc == ')' || yyc == ','))
1215         break;
1216       else if (yyc == '(' || yyc == '[')
1217         ++plev;
1218       else if (yyc == ']' || yyc == ')')
1219         --plev;
1220       else if (yyc == 0)
1221         {
1222           error ("%Hend of file read inside default argument", &starting);
1223           goto done;
1224         }
1225     }
1226
1227   /* Unget the last token.  */
1228   push_token (remove_last_token (buf));
1229
1230  done:
1231 #ifdef SPEW_DEBUG
1232   if (spew_debug)
1233     fprintf (stderr, "\tsaved defarg of %d tokens from %s:%d\n",
1234              buf->limit, starting.file, starting.line);
1235 #endif
1236
1237   arg = make_node (DEFAULT_ARG);
1238   DEFARG_POINTER (arg) = (char *)buf;
1239
1240   return arg;
1241 }
1242
1243 /* Decide whether the default argument we are about to see should be
1244    gobbled up as text for later parsing.  */
1245
1246 void
1247 maybe_snarf_defarg ()
1248 {
1249   if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
1250     do_snarf_defarg = 1;
1251 }
1252
1253 /* Called from grokfndecl to note a function decl with unparsed default
1254    arguments for later processing.  Also called from grokdeclarator
1255    for function types with unparsed defargs; the call from grokfndecl
1256    will always come second, so we can overwrite the entry from the type.  */
1257
1258 void
1259 add_defarg_fn (decl)
1260      tree decl;
1261 {
1262   if (TREE_CODE (decl) == FUNCTION_DECL)
1263     TREE_VALUE (defarg_fns) = decl;
1264   else
1265     {
1266       defarg_fns = tree_cons (NULL_TREE, decl, defarg_fns);
1267       TREE_TYPE (defarg_fns) = current_class_type;
1268     }
1269 }
1270
1271 /* Helper for do_pending_defargs.  Starts the parsing of a default arg.  */
1272
1273 static void
1274 feed_defarg (p)
1275      tree p;
1276 {
1277   tree d = TREE_PURPOSE (p);
1278
1279   feed_input ((struct unparsed_text *)DEFARG_POINTER (d));
1280   yychar = DEFARG_MARKER;
1281   yylval.ttype = p;
1282 }
1283
1284 /* Helper for do_pending_defargs.  Ends the parsing of a default arg.  */
1285
1286 static void
1287 finish_defarg ()
1288 {
1289   if (yychar == YYEMPTY)
1290     yychar = yylex ();
1291   if (yychar != END_OF_SAVED_INPUT)
1292     error ("parse error at end of saved function text");
1293
1294   end_input ();
1295 }
1296
1297 /* Main function for deferred parsing of default arguments.  Called from
1298    the parser.  */
1299
1300 void
1301 do_pending_defargs ()
1302 {
1303   if (defarg_parm)
1304     finish_defarg ();
1305
1306   for (; defarg_fns;)
1307     {
1308       tree current = defarg_fns;
1309
1310       tree defarg_fn = TREE_VALUE (defarg_fns);
1311       if (defarg_parm == NULL_TREE)
1312         {
1313           push_nested_class (TREE_TYPE (defarg_fns), 1);
1314           pushlevel (0);
1315           if (TREE_CODE (defarg_fn) == FUNCTION_DECL)
1316             maybe_begin_member_template_processing (defarg_fn);
1317
1318           if (TREE_CODE (defarg_fn) == FUNCTION_DECL)
1319             defarg_parm = TYPE_ARG_TYPES (TREE_TYPE (defarg_fn));
1320           else
1321             defarg_parm = TYPE_ARG_TYPES (defarg_fn);
1322         }
1323       else
1324         defarg_parm = TREE_CHAIN (defarg_parm);
1325
1326       for (; defarg_parm; defarg_parm = TREE_CHAIN (defarg_parm))
1327         if (!TREE_PURPOSE (defarg_parm)
1328             || TREE_CODE (TREE_PURPOSE (defarg_parm)) != DEFAULT_ARG)
1329           ;/* OK */
1330         else if (TREE_PURPOSE (current) == error_mark_node)
1331           DEFARG_POINTER (TREE_PURPOSE (defarg_parm)) = NULL;
1332         else
1333           {
1334             feed_defarg (defarg_parm);
1335
1336             /* Return to the parser, which will process this defarg
1337                and call us again.  */
1338             return;
1339           }
1340
1341       if (TREE_CODE (defarg_fn) == FUNCTION_DECL)
1342         {
1343           maybe_end_member_template_processing ();
1344           check_default_args (defarg_fn);
1345         }
1346
1347       poplevel (0, 0, 0);
1348       pop_nested_class ();
1349
1350       defarg_fns = TREE_CHAIN (defarg_fns);
1351       if (defarg_depfns)
1352         {
1353           /* This function's default args depend on unprocessed default args
1354              of defarg_fns. We will need to reprocess this function, and
1355              check for circular dependencies.  */
1356           tree a, b;
1357
1358           for (a = defarg_depfns, b = TREE_PURPOSE (current); a && b;
1359                a = TREE_CHAIN (a), b = TREE_CHAIN (b))
1360             if (TREE_VALUE (a) != TREE_VALUE (b))
1361               goto different;
1362           if (a || b)
1363             {
1364             different:;
1365               TREE_CHAIN (current) = NULL_TREE;
1366               defarg_fns = chainon (defarg_fns, current);
1367               TREE_PURPOSE (current) = defarg_depfns;
1368             }
1369           else
1370             {
1371               cp_warning_at ("circular dependency in default args of `%#D'", defarg_fn);
1372               /* No need to say what else is dependent, as they will be
1373                  picked up in another pass.  */
1374
1375               /* Immediately repeat, but marked so that we break the loop. */
1376               defarg_fns = current;
1377               TREE_PURPOSE (current) = error_mark_node;
1378             }
1379           defarg_depfns = NULL_TREE;
1380         }
1381       else if (TREE_PURPOSE (current) == error_mark_node)
1382         defarg_fnsdone = tree_cons (NULL_TREE, defarg_fn, defarg_fnsdone);
1383     }
1384 }
1385
1386 /* After parsing all the default arguments, we must clear any that remain,
1387    which will be part of a circular dependency. */
1388 void
1389 done_pending_defargs ()
1390 {
1391   for (; defarg_fnsdone; defarg_fnsdone = TREE_CHAIN (defarg_fnsdone))
1392     {
1393       tree fn = TREE_VALUE (defarg_fnsdone);
1394       tree parms;
1395
1396       if (TREE_CODE (fn) == FUNCTION_DECL)
1397         parms = TYPE_ARG_TYPES (TREE_TYPE (fn));
1398       else
1399         parms = TYPE_ARG_TYPES (fn);
1400       for (; parms; parms = TREE_CHAIN (parms))
1401         if (TREE_PURPOSE (parms)
1402             && TREE_CODE (TREE_PURPOSE (parms)) == DEFAULT_ARG)
1403           {
1404             my_friendly_assert (!DEFARG_POINTER (TREE_PURPOSE (parms)), 20010107);
1405             TREE_PURPOSE (parms) = NULL_TREE;
1406           }
1407     }
1408 }
1409
1410 /* In processing the current default arg, we called FN, but that call
1411    required a default argument of FN, and that had not yet been processed.
1412    Remember FN.  */
1413
1414 void
1415 unprocessed_defarg_fn (fn)
1416      tree fn;
1417 {
1418   defarg_depfns = tree_cons (NULL_TREE, fn, defarg_depfns);
1419 }
1420
1421 /* Called from the parser to update an element of TYPE_ARG_TYPES for some
1422    FUNCTION_TYPE with the newly parsed version of its default argument, which
1423    was previously digested as text.  */
1424
1425 void
1426 replace_defarg (arg, init)
1427      tree arg, init;
1428 {
1429   if (init == error_mark_node)
1430     TREE_PURPOSE (arg) = error_mark_node;
1431   else
1432     {
1433       if (! processing_template_decl
1434           && ! can_convert_arg (TREE_VALUE (arg), TREE_TYPE (init), init))
1435         pedwarn ("invalid type `%T' for default argument to `%T'",
1436                     TREE_TYPE (init), TREE_VALUE (arg));
1437       if (!defarg_depfns)
1438         TREE_PURPOSE (arg) = init;
1439     }
1440 }
1441
1442 #ifdef SPEW_DEBUG
1443 /* debug_yychar takes a yychar (token number) value and prints its name.  */
1444
1445 static void
1446 debug_yychar (yy)
1447      int yy;
1448 {
1449   if (yy<256)
1450     fprintf (stderr, "->%d < %c >\n", lineno, yy);
1451   else if (yy == IDENTIFIER || yy == tTYPENAME)
1452     {
1453       const char *id;
1454       if (TREE_CODE (yylval.ttype) == IDENTIFIER_NODE)
1455         id = IDENTIFIER_POINTER (yylval.ttype);
1456       else if (TREE_CODE_CLASS (TREE_CODE (yylval.ttype)) == 'd')
1457         id = IDENTIFIER_POINTER (DECL_NAME (yylval.ttype));
1458       else
1459         id = "";
1460       fprintf (stderr, "->%d <%s `%s'>\n", lineno, debug_yytranslate (yy), id);
1461     }
1462   else
1463     fprintf (stderr, "->%d <%s>\n", lineno, debug_yytranslate (yy));
1464 }
1465
1466 #endif
1467
1468 #define NAME(TYPE) cpp_type2name (TYPE)
1469
1470 void
1471 yyerror (msgid)
1472      const char *msgid;
1473 {
1474   const char *string = _(msgid);
1475
1476   if (last_token == CPP_EOF)
1477     error ("%s at end of input", string);
1478   else if (last_token == CPP_CHAR || last_token == CPP_WCHAR)
1479     {
1480       unsigned int val = TREE_INT_CST_LOW (yylval.ttype);
1481       const char *const ell = (last_token == CPP_CHAR) ? "" : "L";
1482       if (val <= UCHAR_MAX && ISGRAPH (val))
1483         error ("%s before %s'%c'", string, ell, val);
1484       else
1485         error ("%s before %s'\\x%x'", string, ell, val);
1486     }
1487   else if (last_token == CPP_STRING
1488            || last_token == CPP_WSTRING)
1489     error ("%s before string constant", string);
1490   else if (last_token == CPP_NUMBER)
1491     error ("%s before numeric constant", string);
1492   else if (last_token == CPP_NAME)
1493     {
1494       if (TREE_CODE (last_token_id) == IDENTIFIER_NODE)
1495         error ("%s before `%s'", string, IDENTIFIER_POINTER (last_token_id));
1496       else if (ISGRAPH (yychar))
1497         error ("%s before `%c'", string, yychar);
1498       else
1499         error ("%s before `\%o'", string, yychar);
1500     }
1501   else
1502     error ("%s before `%s' token", string, NAME (last_token));
1503 }
1504
1505 #include "gt-cp-spew.h"