* spew.c (YYCHAR): Uppercase macro parameter and add
[official-gcc.git] / gcc / cp / spew.c
blob9f36d06b42bab73e23f6770759ec0066051c6155
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)
6 This file is part of GNU CC.
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.
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.
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. */
23 /* This file is the type analyzer for GNU C++. To debug it, define SPEW_DEBUG
24 when compiling parse.c and spew.c. */
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-lex.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"
42 #ifdef SPEW_DEBUG
43 #define SPEW_INLINE
44 #else
45 #define SPEW_INLINE inline
46 #endif
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. */
52 /* fifo of tokens recognized and available to parser. */
53 struct token
55 /* The values for YYCHAR will fit in a short. */
56 short yychar;
57 unsigned int lineno;
58 YYSTYPE yylval;
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.
68 This function's FUNCTION_DECL will have a bit set in its common so
69 that we know to watch out for it. */
71 struct unparsed_text
73 struct unparsed_text *next; /* process this one next */
74 tree decl; /* associated declaration */
75 const char *filename; /* name of file we were processing */
76 int lineno; /* line number we got the text from */
77 int interface; /* remembering interface_unknown and interface_only */
79 struct token *pos; /* current position, when rescanning */
80 struct token *limit; /* end of saved text */
83 /* Stack of state saved off when we return to an inline method or
84 default argument that has been stored for later parsing. */
85 struct feed
87 struct unparsed_text *input;
88 const char *filename;
89 int lineno;
90 int yychar;
91 YYSTYPE yylval;
92 int first_token;
93 struct obstack token_obstack;
94 struct feed *next;
95 };
97 static struct obstack feed_obstack;
98 static struct feed *feed;
100 static SPEW_INLINE void do_aggr PARAMS ((void));
101 static SPEW_INLINE int identifier_type PARAMS ((tree));
102 static void scan_tokens PARAMS ((int));
103 static void feed_defarg PARAMS ((tree));
104 static void finish_defarg PARAMS ((void));
105 static int read_token PARAMS ((struct token *));
107 static SPEW_INLINE int num_tokens PARAMS ((void));
108 static SPEW_INLINE struct token *nth_token PARAMS ((int));
109 static SPEW_INLINE int add_token PARAMS ((struct token *));
110 static SPEW_INLINE int shift_token PARAMS ((void));
111 static SPEW_INLINE void push_token PARAMS ((struct token *));
112 static SPEW_INLINE void consume_token PARAMS ((void));
113 static SPEW_INLINE int read_process_identifier PARAMS ((YYSTYPE *));
115 static SPEW_INLINE void feed_input PARAMS ((struct unparsed_text *));
116 static SPEW_INLINE void snarf_block PARAMS ((const char *, int));
117 static tree snarf_defarg PARAMS ((void));
118 static int frob_id PARAMS ((int, int, tree *));
120 /* The list of inline functions being held off until we reach the end of
121 the current class declaration. */
122 struct unparsed_text *pending_inlines;
123 struct unparsed_text *pending_inlines_tail;
125 /* The list of previously-deferred inline functions currently being parsed.
126 This exists solely to be a GC root. */
127 struct unparsed_text *processing_these_inlines;
129 static void begin_parsing_inclass_inline PARAMS ((struct unparsed_text *));
130 static void mark_pending_inlines PARAMS ((PTR));
132 #ifdef SPEW_DEBUG
133 int spew_debug = 0;
134 static unsigned int yylex_ctr = 0;
136 static void debug_yychar PARAMS ((int));
138 /* In parse.y: */
139 extern char *debug_yytranslate PARAMS ((int));
140 #endif
141 static enum cpp_ttype last_token;
142 static tree last_token_id;
144 /* From lex.c: */
145 /* the declaration found for the last IDENTIFIER token read in.
146 yylex must look this up to detect typedefs, which get token type TYPENAME,
147 so it is left around in case the identifier is not a typedef but is
148 used in a context which makes it a reference to a variable. */
149 extern tree lastiddecl; /* let our brains leak out here too */
150 extern int yychar; /* the lookahead symbol */
151 extern YYSTYPE yylval; /* the semantic value of the */
152 /* lookahead symbol */
153 /* The token fifo lives in this obstack. */
154 struct obstack token_obstack;
155 int first_token;
157 /* Sometimes we need to save tokens for later parsing. If so, they are
158 stored on this obstack. */
159 struct obstack inline_text_obstack;
160 char *inline_text_firstobj;
162 /* When we see a default argument in a method declaration, we snarf it as
163 text using snarf_defarg. When we get up to namespace scope, we then go
164 through and parse all of them using do_pending_defargs. Since yacc
165 parsers are not reentrant, we retain defargs state in these two
166 variables so that subsequent calls to do_pending_defargs can resume
167 where the previous call left off. DEFARG_FNS is a tree_list where
168 the TREE_TYPE is the current_class_type, TREE_VALUE is the FUNCTION_DECL,
169 and TREE_PURPOSE is the list unprocessed dependent functions. */
171 static tree defarg_fns; /* list of functions with unprocessed defargs */
172 static tree defarg_parm; /* current default parameter */
173 static tree defarg_depfns; /* list of unprocessed fns met during current fn. */
174 static tree defarg_fnsdone; /* list of fns with circular defargs */
176 /* Initialize obstacks. Called once, from cxx_init. */
178 void
179 init_spew ()
181 gcc_obstack_init (&inline_text_obstack);
182 inline_text_firstobj = (char *) obstack_alloc (&inline_text_obstack, 0);
183 gcc_obstack_init (&token_obstack);
184 gcc_obstack_init (&feed_obstack);
185 ggc_add_tree_root (&defarg_fns, 1);
186 ggc_add_tree_root (&defarg_parm, 1);
187 ggc_add_tree_root (&defarg_depfns, 1);
188 ggc_add_tree_root (&defarg_fnsdone, 1);
190 ggc_add_root (&pending_inlines, 1, sizeof (struct unparsed_text *),
191 mark_pending_inlines);
192 ggc_add_root (&processing_these_inlines, 1, sizeof (struct unparsed_text *),
193 mark_pending_inlines);
196 void
197 clear_inline_text_obstack ()
199 obstack_free (&inline_text_obstack, inline_text_firstobj);
202 /* Subroutine of read_token. */
203 static SPEW_INLINE int
204 read_process_identifier (pyylval)
205 YYSTYPE *pyylval;
207 tree id = pyylval->ttype;
209 if (C_IS_RESERVED_WORD (id))
211 /* Possibly replace the IDENTIFIER_NODE with a magic cookie.
212 Can't put yylval.code numbers in ridpointers[]. Bleah. */
214 switch (C_RID_CODE (id))
216 case RID_BITAND: pyylval->code = BIT_AND_EXPR; return '&';
217 case RID_AND_EQ: pyylval->code = BIT_AND_EXPR; return ASSIGN;
218 case RID_BITOR: pyylval->code = BIT_IOR_EXPR; return '|';
219 case RID_OR_EQ: pyylval->code = BIT_IOR_EXPR; return ASSIGN;
220 case RID_XOR: pyylval->code = BIT_XOR_EXPR; return '^';
221 case RID_XOR_EQ: pyylval->code = BIT_XOR_EXPR; return ASSIGN;
222 case RID_NOT_EQ: pyylval->code = NE_EXPR; return EQCOMPARE;
224 default:
225 if (C_RID_YYCODE (id) == TYPESPEC)
226 GNU_xref_ref (current_function_decl, IDENTIFIER_POINTER (id));
228 pyylval->ttype = ridpointers[C_RID_CODE (id)];
229 return C_RID_YYCODE (id);
233 GNU_xref_ref (current_function_decl, IDENTIFIER_POINTER (id));
235 /* Make sure that user does not collide with our internal naming
236 scheme. This is not necessary if '.' is used to remove them from
237 the user's namespace, but is if '$' or double underscores are. */
239 #if !defined(JOINER) || JOINER == '$'
240 if (VPTR_NAME_P (id)
241 || VTABLE_NAME_P (id)
242 || TEMP_NAME_P (id)
243 || ANON_AGGRNAME_P (id))
244 warning (
245 "identifier name `%s' conflicts with GNU C++ internal naming strategy",
246 IDENTIFIER_POINTER (id));
247 #endif
248 return IDENTIFIER;
251 /* Read the next token from the input file. The token is written into
252 T, and its type number is returned. */
253 static int
254 read_token (t)
255 struct token *t;
257 retry:
259 last_token = c_lex (&last_token_id);
260 t->yylval.ttype = last_token_id;
262 switch (last_token)
264 #define YYCHAR(YY) t->yychar = (YY); break;
265 #define YYCODE(C) t->yylval.code = (C);
267 case CPP_EQ: YYCHAR('=');
268 case CPP_NOT: YYCHAR('!');
269 case CPP_GREATER: YYCODE(GT_EXPR); YYCHAR('>');
270 case CPP_LESS: YYCODE(LT_EXPR); YYCHAR('<');
271 case CPP_PLUS: YYCODE(PLUS_EXPR); YYCHAR('+');
272 case CPP_MINUS: YYCODE(MINUS_EXPR); YYCHAR('-');
273 case CPP_MULT: YYCODE(MULT_EXPR); YYCHAR('*');
274 case CPP_DIV: YYCODE(TRUNC_DIV_EXPR); YYCHAR('/');
275 case CPP_MOD: YYCODE(TRUNC_MOD_EXPR); YYCHAR('%');
276 case CPP_AND: YYCODE(BIT_AND_EXPR); YYCHAR('&');
277 case CPP_OR: YYCODE(BIT_IOR_EXPR); YYCHAR('|');
278 case CPP_XOR: YYCODE(BIT_XOR_EXPR); YYCHAR('^');
279 case CPP_RSHIFT: YYCODE(RSHIFT_EXPR); YYCHAR(RSHIFT);
280 case CPP_LSHIFT: YYCODE(LSHIFT_EXPR); YYCHAR(LSHIFT);
282 case CPP_COMPL: YYCHAR('~');
283 case CPP_AND_AND: YYCHAR(ANDAND);
284 case CPP_OR_OR: YYCHAR(OROR);
285 case CPP_QUERY: YYCHAR('?');
286 case CPP_COLON: YYCHAR(':');
287 case CPP_COMMA: YYCHAR(',');
288 case CPP_OPEN_PAREN: YYCHAR('(');
289 case CPP_CLOSE_PAREN: YYCHAR(')');
290 case CPP_EQ_EQ: YYCODE(EQ_EXPR); YYCHAR(EQCOMPARE);
291 case CPP_NOT_EQ: YYCODE(NE_EXPR); YYCHAR(EQCOMPARE);
292 case CPP_GREATER_EQ:YYCODE(GE_EXPR); YYCHAR(ARITHCOMPARE);
293 case CPP_LESS_EQ: YYCODE(LE_EXPR); YYCHAR(ARITHCOMPARE);
295 case CPP_PLUS_EQ: YYCODE(PLUS_EXPR); YYCHAR(ASSIGN);
296 case CPP_MINUS_EQ: YYCODE(MINUS_EXPR); YYCHAR(ASSIGN);
297 case CPP_MULT_EQ: YYCODE(MULT_EXPR); YYCHAR(ASSIGN);
298 case CPP_DIV_EQ: YYCODE(TRUNC_DIV_EXPR); YYCHAR(ASSIGN);
299 case CPP_MOD_EQ: YYCODE(TRUNC_MOD_EXPR); YYCHAR(ASSIGN);
300 case CPP_AND_EQ: YYCODE(BIT_AND_EXPR); YYCHAR(ASSIGN);
301 case CPP_OR_EQ: YYCODE(BIT_IOR_EXPR); YYCHAR(ASSIGN);
302 case CPP_XOR_EQ: YYCODE(BIT_XOR_EXPR); YYCHAR(ASSIGN);
303 case CPP_RSHIFT_EQ: YYCODE(RSHIFT_EXPR); YYCHAR(ASSIGN);
304 case CPP_LSHIFT_EQ: YYCODE(LSHIFT_EXPR); YYCHAR(ASSIGN);
306 case CPP_OPEN_SQUARE: YYCHAR('[');
307 case CPP_CLOSE_SQUARE: YYCHAR(']');
308 case CPP_OPEN_BRACE: YYCHAR('{');
309 case CPP_CLOSE_BRACE: YYCHAR('}');
310 case CPP_SEMICOLON: YYCHAR(';');
311 case CPP_ELLIPSIS: YYCHAR(ELLIPSIS);
313 case CPP_PLUS_PLUS: YYCHAR(PLUSPLUS);
314 case CPP_MINUS_MINUS: YYCHAR(MINUSMINUS);
315 case CPP_DEREF: YYCHAR(POINTSAT);
316 case CPP_DOT: YYCHAR('.');
318 /* These tokens are C++ specific. */
319 case CPP_SCOPE: YYCHAR(SCOPE);
320 case CPP_DEREF_STAR: YYCHAR(POINTSAT_STAR);
321 case CPP_DOT_STAR: YYCHAR(DOT_STAR);
322 case CPP_MIN_EQ: YYCODE(MIN_EXPR); YYCHAR(ASSIGN);
323 case CPP_MAX_EQ: YYCODE(MAX_EXPR); YYCHAR(ASSIGN);
324 case CPP_MIN: YYCODE(MIN_EXPR); YYCHAR(MIN_MAX);
325 case CPP_MAX: YYCODE(MAX_EXPR); YYCHAR(MIN_MAX);
326 #undef YYCHAR
327 #undef YYCODE
329 case CPP_EOF:
330 t->yychar = 0;
331 break;
333 case CPP_NAME:
334 t->yychar = read_process_identifier (&t->yylval);
335 break;
337 case CPP_NUMBER:
338 case CPP_CHAR:
339 case CPP_WCHAR:
340 t->yychar = CONSTANT;
341 break;
343 case CPP_STRING:
344 case CPP_WSTRING:
345 t->yychar = STRING;
346 break;
348 default:
349 yyerror ("parse error");
350 goto retry;
353 t->lineno = lineno;
354 return t->yychar;
357 static void
358 feed_input (input)
359 struct unparsed_text *input;
361 struct feed *f;
362 #if 0
363 if (feed)
364 abort ();
365 #endif
367 f = obstack_alloc (&feed_obstack, sizeof (struct feed));
369 /* The token list starts just after the struct unparsed_text in memory. */
370 input->pos = (struct token *) (input + 1);
372 #ifdef SPEW_DEBUG
373 if (spew_debug)
374 fprintf (stderr, "\tfeeding %s:%d [%d tokens]\n",
375 input->filename, input->lineno, input->limit - input->pos);
376 #endif
378 f->input = input;
379 f->filename = input_filename;
380 f->lineno = lineno;
381 f->yychar = yychar;
382 f->yylval = yylval;
383 f->first_token = first_token;
384 f->token_obstack = token_obstack;
385 f->next = feed;
387 input_filename = input->filename;
388 lineno = input->lineno;
389 yychar = YYEMPTY;
390 yylval.ttype = NULL_TREE;
391 first_token = 0;
392 gcc_obstack_init (&token_obstack);
393 feed = f;
396 void
397 end_input ()
399 struct feed *f = feed;
401 input_filename = f->filename;
402 lineno = f->lineno;
403 yychar = f->yychar;
404 yylval = f->yylval;
405 first_token = f->first_token;
406 obstack_free (&token_obstack, 0);
407 token_obstack = f->token_obstack;
408 feed = f->next;
410 obstack_free (&feed_obstack, f);
412 #ifdef SPEW_DEBUG
413 if (spew_debug)
414 fprintf (stderr, "\treturning to %s:%d\n", input_filename, lineno);
415 #endif
418 /* GC callback to mark memory pointed to by the pending inline queue. */
419 static void
420 mark_pending_inlines (pi)
421 PTR pi;
423 struct unparsed_text *up = * (struct unparsed_text **)pi;
425 while (up)
427 struct token *t = (struct token *) (up + 1);
428 struct token *l = up->limit;
430 while (t < l)
432 /* Some of the possible values for yychar use yylval.code
433 instead of yylval.ttype. We only have to worry about
434 yychars that could have been returned by read_token. */
435 switch (t->yychar)
437 case '+': case '-': case '*': case '/':
438 case '%': case '&': case '|': case '^':
439 case '>': case '<': case LSHIFT: case RSHIFT:
440 case ASSIGN: case MIN_MAX: case EQCOMPARE: case ARITHCOMPARE:
441 t++;
442 continue;
444 if (t->yylval.ttype)
445 ggc_mark_tree (t->yylval.ttype);
446 t++;
448 up = up->next;
452 /* Token queue management. */
454 /* Return the number of tokens available on the fifo. */
455 static SPEW_INLINE int
456 num_tokens ()
458 return (obstack_object_size (&token_obstack) / sizeof (struct token))
459 - first_token;
462 /* Fetch the token N down the line from the head of the fifo. */
464 static SPEW_INLINE struct token*
465 nth_token (n)
466 int n;
468 #ifdef ENABLE_CHECKING
469 /* could just have this do slurp_ implicitly, but this way is easier
470 to debug... */
471 my_friendly_assert (n >= 0 && n < num_tokens (), 298);
472 #endif
473 return ((struct token*)obstack_base (&token_obstack)) + n + first_token;
476 static const struct token Teosi = { END_OF_SAVED_INPUT, 0 UNION_INIT_ZERO };
477 static const struct token Tpad = { EMPTY, 0 UNION_INIT_ZERO };
479 /* Copy the next token into T and return its value. */
480 static SPEW_INLINE int
481 add_token (t)
482 struct token *t;
484 if (!feed)
485 return read_token (t);
487 if (feed->input->pos < feed->input->limit)
489 memcpy (t, feed->input->pos, sizeof (struct token));
490 return (feed->input->pos++)->yychar;
493 memcpy (t, &Teosi, sizeof (struct token));
494 return END_OF_SAVED_INPUT;
497 /* Shift the next token onto the fifo. */
498 static SPEW_INLINE int
499 shift_token ()
501 size_t point = obstack_object_size (&token_obstack);
502 obstack_blank (&token_obstack, sizeof (struct token));
503 return add_token ((struct token *) (obstack_base (&token_obstack) + point));
506 /* Consume the next token out of the fifo. */
508 static SPEW_INLINE void
509 consume_token ()
511 if (num_tokens () == 1)
513 obstack_free (&token_obstack, obstack_base (&token_obstack));
514 first_token = 0;
516 else
517 first_token++;
520 /* Push a token at the head of the queue; it will be the next token read. */
521 static SPEW_INLINE void
522 push_token (t)
523 struct token *t;
525 if (first_token == 0) /* We hope this doesn't happen often. */
527 size_t active = obstack_object_size (&token_obstack);
528 obstack_blank (&token_obstack, sizeof (struct token));
529 if (active)
530 memmove (obstack_base (&token_obstack) + sizeof (struct token),
531 obstack_base (&token_obstack), active);
532 first_token++;
534 first_token--;
535 memcpy (nth_token (0), t, sizeof (struct token));
539 /* Pull in enough tokens that the queue is N long beyond the current
540 token. */
542 static void
543 scan_tokens (n)
544 int n;
546 int i;
547 int num = num_tokens ();
548 int yychar;
550 /* First, prune any empty tokens at the end. */
551 i = num;
552 while (i > 0 && nth_token (i - 1)->yychar == EMPTY)
553 i--;
554 if (i < num)
556 obstack_blank (&token_obstack, -((num - i) * sizeof (struct token)));
557 num = i;
560 /* Now, if we already have enough tokens, return. */
561 if (num > n)
562 return;
564 /* Never read past these characters: they might separate
565 the current input stream from one we save away later. */
566 for (i = 0; i < num; i++)
568 yychar = nth_token (i)->yychar;
569 if (yychar == '{' || yychar == ':' || yychar == ';')
570 goto pad_tokens;
573 while (num_tokens () <= n)
575 yychar = shift_token ();
576 if (yychar == '{' || yychar == ':' || yychar == ';')
577 goto pad_tokens;
579 return;
581 pad_tokens:
582 while (num_tokens () <= n)
583 obstack_grow (&token_obstack, &Tpad, sizeof (struct token));
586 int looking_for_typename;
587 int looking_for_template;
589 static int after_friend;
590 static int after_new;
591 static int do_snarf_defarg;
593 tree got_scope;
594 tree got_object;
596 static SPEW_INLINE int
597 identifier_type (decl)
598 tree decl;
600 tree t;
602 if (TREE_CODE (decl) == TEMPLATE_DECL)
604 if (TREE_CODE (DECL_TEMPLATE_RESULT (decl)) == TYPE_DECL)
605 return PTYPENAME;
606 else if (looking_for_template)
607 return PFUNCNAME;
609 if (looking_for_template && really_overloaded_fn (decl))
611 /* See through a baselink. */
612 if (TREE_CODE (decl) == TREE_LIST)
613 decl = TREE_VALUE (decl);
615 for (t = decl; t != NULL_TREE; t = OVL_CHAIN (t))
616 if (DECL_FUNCTION_TEMPLATE_P (OVL_FUNCTION (t)))
617 return PFUNCNAME;
619 if (TREE_CODE (decl) == NAMESPACE_DECL)
620 return NSNAME;
621 if (TREE_CODE (decl) != TYPE_DECL)
622 return IDENTIFIER;
623 if (DECL_ARTIFICIAL (decl) && TREE_TYPE (decl) == current_class_type)
624 return SELFNAME;
626 /* A constructor declarator for a template type will get here as an
627 implicit typename, a TYPENAME_TYPE with a type. */
628 t = got_scope;
629 if (t && TREE_CODE (t) == TYPENAME_TYPE)
630 t = TREE_TYPE (t);
631 decl = TREE_TYPE (decl);
632 if (TREE_CODE (decl) == TYPENAME_TYPE)
633 decl = TREE_TYPE (decl);
634 if (t && t == decl)
635 return SELFNAME;
637 return TYPENAME;
640 /* token[0] == AGGR (struct/union/enum)
641 Thus, token[1] is either a TYPENAME or a TYPENAME_DEFN.
642 If token[2] == '{' or ':' then it's TYPENAME_DEFN.
643 It's also a definition if it's a forward declaration (as in 'struct Foo;')
644 which we can tell if token[2] == ';' *and* token[-1] != FRIEND or NEW. */
646 static SPEW_INLINE void
647 do_aggr ()
649 int yc1, yc2;
651 scan_tokens (2);
652 yc1 = nth_token (1)->yychar;
653 if (yc1 != TYPENAME && yc1 != IDENTIFIER && yc1 != PTYPENAME)
654 return;
655 yc2 = nth_token (2)->yychar;
656 if (yc2 == ';')
658 /* It's a forward declaration iff we were not preceded by
659 'friend' or `new'. */
660 if (after_friend || after_new)
661 return;
663 else if (yc2 != '{' && yc2 != ':')
664 return;
666 switch (yc1)
668 case TYPENAME:
669 nth_token (1)->yychar = TYPENAME_DEFN;
670 break;
671 case PTYPENAME:
672 nth_token (1)->yychar = PTYPENAME_DEFN;
673 break;
674 case IDENTIFIER:
675 nth_token (1)->yychar = IDENTIFIER_DEFN;
676 break;
677 default:
678 my_friendly_abort (102);
682 void
683 see_typename ()
685 /* Only types expected, not even namespaces. */
686 looking_for_typename = 2;
687 if (yychar < 0)
688 if ((yychar = yylex ()) < 0) yychar = 0;
689 looking_for_typename = 0;
690 if (yychar == IDENTIFIER)
692 lastiddecl = lookup_name (yylval.ttype, -2);
693 if (lastiddecl)
694 yychar = identifier_type (lastiddecl);
699 yylex ()
701 int yychr;
702 int old_looking_for_typename = 0;
703 int just_saw_new = 0;
704 int just_saw_friend = 0;
706 timevar_push (TV_LEX);
708 retry:
709 #ifdef SPEW_DEBUG
710 if (spew_debug)
712 yylex_ctr ++;
713 fprintf (stderr, "\t\t## %d @%d ", yylex_ctr, lineno);
715 #endif
717 if (do_snarf_defarg)
719 do_snarf_defarg = 0;
720 yylval.ttype = snarf_defarg ();
721 yychar = DEFARG;
722 got_object = NULL_TREE;
723 timevar_pop (TV_LEX);
724 return DEFARG;
727 /* if we've got tokens, send them */
728 else if (num_tokens ())
729 yychr = nth_token (0)->yychar;
730 else
731 yychr = shift_token ();
733 /* many tokens just need to be returned. At first glance, all we
734 have to do is send them back up, but some of them are needed to
735 figure out local context. */
736 switch (yychr)
738 case EMPTY:
739 /* This is a lexical no-op. */
740 #ifdef SPEW_DEBUG
741 if (spew_debug)
742 debug_yychar (yychr);
743 #endif
744 consume_token ();
745 goto retry;
747 case '(':
748 scan_tokens (1);
749 if (nth_token (1)->yychar == ')')
751 consume_token ();
752 yychr = LEFT_RIGHT;
754 break;
756 case IDENTIFIER:
758 int peek;
760 scan_tokens (1);
761 peek = nth_token (1)->yychar;
762 yychr = frob_id (yychr, peek, &nth_token (0)->yylval.ttype);
763 break;
765 case IDENTIFIER_DEFN:
766 case TYPENAME:
767 case TYPENAME_DEFN:
768 case PTYPENAME:
769 case PTYPENAME_DEFN:
770 /* If we see a SCOPE next, restore the old value.
771 Otherwise, we got what we want. */
772 looking_for_typename = old_looking_for_typename;
773 looking_for_template = 0;
774 break;
776 case SCSPEC:
777 if (nth_token (0)->yylval.ttype == ridpointers[RID_EXTERN])
779 scan_tokens (1);
780 if (nth_token (1)->yychar == STRING)
782 yychr = EXTERN_LANG_STRING;
783 nth_token (1)->yylval.ttype = get_identifier
784 (TREE_STRING_POINTER (nth_token (1)->yylval.ttype));
785 consume_token ();
788 /* do_aggr needs to know if the previous token was `friend'. */
789 else if (nth_token (0)->yylval.ttype == ridpointers[RID_FRIEND])
790 just_saw_friend = 1;
792 break;
794 case NEW:
795 /* do_aggr needs to know if the previous token was `new'. */
796 just_saw_new = 1;
797 break;
799 case TYPESPEC:
800 case '{':
801 case ':':
802 case ';':
803 /* If this provides a type for us, then revert lexical
804 state to standard state. */
805 looking_for_typename = 0;
806 break;
808 case AGGR:
809 do_aggr ();
810 break;
812 case ENUM:
813 /* Set this again, in case we are rescanning. */
814 looking_for_typename = 2;
815 break;
817 default:
818 break;
821 after_friend = just_saw_friend;
822 after_new = just_saw_new;
824 /* class member lookup only applies to the first token after the object
825 expression, except for explicit destructor calls. */
826 if (yychr != '~')
827 got_object = NULL_TREE;
829 yychar = yychr;
831 struct token *tok = nth_token (0);
833 yylval = tok->yylval;
834 if (tok->lineno)
835 lineno = tok->lineno;
838 #ifdef SPEW_DEBUG
839 if (spew_debug)
840 debug_yychar (yychr);
841 #endif
842 consume_token ();
844 timevar_pop (TV_LEX);
845 return yychr;
848 /* Unget character CH from the input stream.
849 If RESCAN is non-zero, then we want to `see' this
850 character as the next input token. */
852 void
853 yyungetc (ch, rescan)
854 int ch;
855 int rescan;
857 /* Unget a character from the input stream. */
858 if (yychar == YYEMPTY || rescan == 0)
860 struct token fake;
862 /* If we're putting back a brace, undo the change in indent_level
863 from the first time we saw it. */
864 if (ch == '{')
865 indent_level--;
866 else if (ch == '}')
867 indent_level++;
869 fake.yychar = ch;
870 fake.yylval.ttype = 0;
871 fake.lineno = lineno;
873 push_token (&fake);
875 else
877 yychar = ch;
881 /* Lexer hackery to determine what *IDP really is. */
883 static int
884 frob_id (yyc, peek, idp)
885 int yyc;
886 int peek;
887 tree *idp;
889 tree trrr;
890 int old_looking_for_typename = 0;
892 if (peek == SCOPE)
894 /* Don't interfere with the setting from an 'aggr' prefix. */
895 old_looking_for_typename = looking_for_typename;
896 looking_for_typename = 1;
898 else if (peek == '<')
899 looking_for_template = 1;
900 trrr = lookup_name (*idp, -2);
901 if (trrr)
903 yyc = identifier_type (trrr);
904 switch(yyc)
906 case TYPENAME:
907 case SELFNAME:
908 case NSNAME:
909 case PTYPENAME:
910 /* If this got special lookup, remember it. In these
911 cases, we know it can't be a declarator-id. */
912 if (got_scope || got_object)
913 *idp = trrr;
914 /* FALLTHROUGH */
915 case PFUNCNAME:
916 case IDENTIFIER:
917 lastiddecl = trrr;
918 break;
919 default:
920 my_friendly_abort (20000907);
923 else
924 lastiddecl = NULL_TREE;
925 got_scope = NULL_TREE;
926 looking_for_typename = old_looking_for_typename;
927 looking_for_template = 0;
928 return yyc;
931 /* ID is an operator name. Duplicate the hackery in yylex to determine what
932 it really is. */
934 tree frob_opname (id)
935 tree id;
937 scan_tokens (0);
938 frob_id (0, nth_token (0)->yychar, &id);
939 got_object = NULL_TREE;
940 return id;
943 /* Set up the state required to correctly handle the definition of the
944 inline function whose preparsed state has been saved in PI. */
946 static void
947 begin_parsing_inclass_inline (pi)
948 struct unparsed_text *pi;
950 tree context;
952 /* Record that we are processing the chain of inlines starting at
953 PI in a special GC root. */
954 processing_these_inlines = pi;
956 ggc_collect ();
958 /* If this is an inline function in a local class, we must make sure
959 that we save all pertinent information about the function
960 surrounding the local class. */
961 context = decl_function_context (pi->decl);
962 if (context)
963 push_function_context_to (context);
965 feed_input (pi);
966 interface_unknown = pi->interface == 1;
967 interface_only = pi->interface == 0;
968 DECL_PENDING_INLINE_P (pi->decl) = 0;
969 DECL_PENDING_INLINE_INFO (pi->decl) = 0;
971 /* Pass back a handle to the rest of the inline functions, so that they
972 can be processed later. */
973 yychar = PRE_PARSED_FUNCTION_DECL;
974 yylval.pi = pi;
976 start_function (NULL_TREE, pi->decl, NULL_TREE,
977 (SF_DEFAULT | SF_PRE_PARSED | SF_INCLASS_INLINE));
980 /* Called from the top level: if there are any pending inlines to
981 do, set up to process them now. This function sets up the first function
982 to be parsed; after it has been, the rule for fndef in parse.y will
983 call process_next_inline to start working on the next one. */
985 void
986 do_pending_inlines ()
988 /* Oops, we're still dealing with the last batch. */
989 if (yychar == PRE_PARSED_FUNCTION_DECL)
990 return;
992 if (pending_inlines)
994 /* Clear the chain, so that any inlines nested inside the batch
995 we're to process now don't refer to this batch. See e.g.
996 g++.other/lookup6.C. */
997 struct unparsed_text *first = pending_inlines;
998 pending_inlines = pending_inlines_tail = 0;
1000 begin_parsing_inclass_inline (first);
1004 /* Called from the fndecl rule in the parser when the function just parsed
1005 was declared using a PRE_PARSED_FUNCTION_DECL (i.e. came from
1006 do_pending_inlines). */
1008 void
1009 process_next_inline (i)
1010 struct unparsed_text *i;
1012 tree decl = i->decl;
1013 tree context = decl_function_context (decl);
1015 if (context)
1016 pop_function_context_from (context);
1017 if (yychar == YYEMPTY)
1018 yychar = yylex ();
1019 if (yychar != END_OF_SAVED_INPUT)
1020 error ("parse error at end of saved function text");
1021 end_input ();
1023 i = i->next;
1024 if (i)
1025 begin_parsing_inclass_inline (i);
1026 else
1028 processing_these_inlines = 0;
1029 extract_interface_info ();
1034 /* Subroutine of snarf_method, deals with actual absorption of the block. */
1036 static SPEW_INLINE void
1037 snarf_block (starting_file, starting_line)
1038 const char *starting_file;
1039 int starting_line;
1041 int blev = 1;
1042 int look_for_semicolon = 0;
1043 int look_for_lbrac = 0;
1044 int look_for_catch = 0;
1045 int yyc;
1046 struct token tmp;
1047 size_t point;
1049 if (yychar == '{')
1050 /* We incremented indent_level in yylex; undo that. */
1051 indent_level--;
1052 else if (yychar == '=')
1053 look_for_semicolon = 1;
1054 else if (yychar == ':' || yychar == RETURN_KEYWORD || yychar == TRY)
1056 if (yychar == TRY)
1057 look_for_catch = 1;
1058 look_for_lbrac = 1;
1059 blev = 0;
1061 else
1062 yyerror ("parse error in method specification");
1064 /* The current token is the first one to be recorded. */
1065 tmp.yychar = yychar;
1066 tmp.yylval = yylval;
1067 tmp.lineno = lineno;
1068 obstack_grow (&inline_text_obstack, &tmp, sizeof (struct token));
1070 for (;;)
1072 point = obstack_object_size (&inline_text_obstack);
1073 obstack_blank (&inline_text_obstack, sizeof (struct token));
1074 yyc = add_token ((struct token *)
1075 (obstack_base (&inline_text_obstack) + point));
1077 if (yyc == '{')
1079 look_for_lbrac = 0;
1080 blev++;
1082 else if (yyc == '}')
1084 blev--;
1085 if (blev == 0 && !look_for_semicolon)
1087 if (!look_for_catch)
1088 break;
1090 if (add_token (&tmp) != CATCH)
1092 push_token (&tmp);
1093 break;
1096 look_for_lbrac = 1;
1097 obstack_grow (&inline_text_obstack, &tmp, sizeof (struct token));
1100 else if (yyc == ';')
1102 if (look_for_lbrac)
1104 error ("function body for constructor missing");
1105 /* fake a { } to avoid further errors */
1106 tmp.yylval.ttype = 0;
1107 tmp.yychar = '{';
1108 obstack_grow (&inline_text_obstack, &tmp, sizeof (struct token));
1109 tmp.yychar = '}';
1110 obstack_grow (&inline_text_obstack, &tmp, sizeof (struct token));
1111 break;
1113 else if (look_for_semicolon && blev == 0)
1114 break;
1116 else if (yyc == 0)
1118 error_with_file_and_line (starting_file, starting_line,
1119 "end of file read inside definition");
1120 break;
1125 /* This function stores away the text for an inline function that should
1126 be processed later (by do_pending_inlines). */
1127 void
1128 snarf_method (decl)
1129 tree decl;
1131 int starting_lineno = lineno;
1132 const char *starting_filename = input_filename;
1133 size_t len;
1135 struct unparsed_text *meth;
1137 /* Leave room for the header, then absorb the block. */
1138 obstack_blank (&inline_text_obstack, sizeof (struct unparsed_text));
1139 snarf_block (starting_filename, starting_lineno);
1141 len = obstack_object_size (&inline_text_obstack);
1142 meth = (struct unparsed_text *) obstack_finish (&inline_text_obstack);
1144 /* Happens when we get two declarations of the same function in the
1145 same scope. */
1146 if (decl == void_type_node
1147 || (current_class_type && TYPE_REDEFINED (current_class_type)))
1149 obstack_free (&inline_text_obstack, (char *)meth);
1150 return;
1153 meth->decl = decl;
1154 meth->filename = starting_filename;
1155 meth->lineno = starting_lineno;
1156 meth->limit = (struct token *) ((char *)meth + len);
1157 meth->interface = (interface_unknown ? 1 : (interface_only ? 0 : 2));
1158 meth->next = 0;
1160 #ifdef SPEW_DEBUG
1161 if (spew_debug)
1162 fprintf (stderr, "\tsaved method of %d tokens from %s:%d\n",
1163 meth->limit - (struct token *) (meth + 1),
1164 starting_filename, starting_lineno);
1165 #endif
1167 DECL_PENDING_INLINE_INFO (decl) = meth;
1168 DECL_PENDING_INLINE_P (decl) = 1;
1170 if (pending_inlines_tail)
1171 pending_inlines_tail->next = meth;
1172 else
1173 pending_inlines = meth;
1174 pending_inlines_tail = meth;
1177 /* Consume a no-commas expression - a default argument - and save it
1178 on the inline_text_obstack. */
1180 static tree
1181 snarf_defarg ()
1183 int starting_lineno = lineno;
1184 const char *starting_filename = input_filename;
1185 int yyc;
1186 int plev = 0;
1187 size_t point;
1188 size_t len;
1189 struct unparsed_text *buf;
1190 tree arg;
1192 obstack_blank (&inline_text_obstack, sizeof (struct unparsed_text));
1194 for (;;)
1196 point = obstack_object_size (&inline_text_obstack);
1197 obstack_blank (&inline_text_obstack, sizeof (struct token));
1198 yyc = add_token ((struct token *)
1199 (obstack_base (&inline_text_obstack) + point));
1201 if (plev <= 0 && (yyc == ')' || yyc == ','))
1202 break;
1203 else if (yyc == '(' || yyc == '[')
1204 ++plev;
1205 else if (yyc == ']' || yyc == ')')
1206 --plev;
1207 else if (yyc == 0)
1209 error_with_file_and_line (starting_filename, starting_lineno,
1210 "end of file read inside default argument");
1211 goto done;
1215 /* Unget the last token. */
1216 push_token ((struct token *) (obstack_base (&inline_text_obstack) + point));
1217 /* This is the documented way to shrink a growing obstack block. */
1218 obstack_blank (&inline_text_obstack, - (int) sizeof (struct token));
1220 done:
1221 len = obstack_object_size (&inline_text_obstack);
1222 buf = (struct unparsed_text *) obstack_finish (&inline_text_obstack);
1224 buf->decl = 0;
1225 buf->filename = starting_filename;
1226 buf->lineno = starting_lineno;
1227 buf->limit = (struct token *) ((char *)buf + len);
1228 buf->next = 0;
1230 #ifdef SPEW_DEBUG
1231 if (spew_debug)
1232 fprintf (stderr, "\tsaved defarg of %d tokens from %s:%d\n",
1233 buf->limit - (struct token *) (buf + 1),
1234 starting_filename, starting_lineno);
1235 #endif
1237 arg = make_node (DEFAULT_ARG);
1238 DEFARG_POINTER (arg) = (char *)buf;
1240 return arg;
1243 /* Decide whether the default argument we are about to see should be
1244 gobbled up as text for later parsing. */
1246 void
1247 maybe_snarf_defarg ()
1249 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
1250 do_snarf_defarg = 1;
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. */
1258 void
1259 add_defarg_fn (decl)
1260 tree decl;
1262 if (TREE_CODE (decl) == FUNCTION_DECL)
1263 TREE_VALUE (defarg_fns) = decl;
1264 else
1266 defarg_fns = tree_cons (NULL_TREE, decl, defarg_fns);
1267 TREE_TYPE (defarg_fns) = current_class_type;
1271 /* Helper for do_pending_defargs. Starts the parsing of a default arg. */
1273 static void
1274 feed_defarg (p)
1275 tree p;
1277 tree d = TREE_PURPOSE (p);
1279 feed_input ((struct unparsed_text *)DEFARG_POINTER (d));
1280 yychar = DEFARG_MARKER;
1281 yylval.ttype = p;
1284 /* Helper for do_pending_defargs. Ends the parsing of a default arg. */
1286 static void
1287 finish_defarg ()
1289 if (yychar == YYEMPTY)
1290 yychar = yylex ();
1291 if (yychar != END_OF_SAVED_INPUT)
1292 error ("parse error at end of saved function text");
1294 end_input ();
1297 /* Main function for deferred parsing of default arguments. Called from
1298 the parser. */
1300 void
1301 do_pending_defargs ()
1303 if (defarg_parm)
1304 finish_defarg ();
1306 for (; defarg_fns;)
1308 tree current = defarg_fns;
1310 tree defarg_fn = TREE_VALUE (defarg_fns);
1311 if (defarg_parm == NULL_TREE)
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);
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);
1323 else
1324 defarg_parm = TREE_CHAIN (defarg_parm);
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
1334 feed_defarg (defarg_parm);
1336 /* Return to the parser, which will process this defarg
1337 and call us again. */
1338 return;
1341 if (TREE_CODE (defarg_fn) == FUNCTION_DECL)
1343 maybe_end_member_template_processing ();
1344 check_default_args (defarg_fn);
1347 poplevel (0, 0, 0);
1348 pop_nested_class ();
1350 defarg_fns = TREE_CHAIN (defarg_fns);
1351 if (defarg_depfns)
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;
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)
1364 different:;
1365 TREE_CHAIN (current) = NULL_TREE;
1366 defarg_fns = chainon (defarg_fns, current);
1367 TREE_PURPOSE (current) = defarg_depfns;
1369 else
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. */
1375 /* Immediately repeat, but marked so that we break the loop. */
1376 defarg_fns = current;
1377 TREE_PURPOSE (current) = error_mark_node;
1379 defarg_depfns = NULL_TREE;
1381 else if (TREE_PURPOSE (current) == error_mark_node)
1382 defarg_fnsdone = tree_cons (NULL_TREE, defarg_fn, defarg_fnsdone);
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 ()
1391 for (; defarg_fnsdone; defarg_fnsdone = TREE_CHAIN (defarg_fnsdone))
1393 tree fn = TREE_VALUE (defarg_fnsdone);
1394 tree parms;
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)
1404 my_friendly_assert (!DEFARG_POINTER (TREE_PURPOSE (parms)), 20010107);
1405 TREE_PURPOSE (parms) = NULL_TREE;
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. */
1414 void
1415 unprocessed_defarg_fn (fn)
1416 tree fn;
1418 defarg_depfns = tree_cons (NULL_TREE, fn, defarg_depfns);
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. */
1425 void
1426 replace_defarg (arg, init)
1427 tree arg, init;
1429 if (init == error_mark_node)
1430 TREE_PURPOSE (arg) = error_mark_node;
1431 else
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;
1442 #ifdef SPEW_DEBUG
1443 /* debug_yychar takes a yychar (token number) value and prints its name. */
1445 static void
1446 debug_yychar (yy)
1447 int yy;
1449 if (yy<256)
1450 fprintf (stderr, "->%d < %c >\n", lineno, yy);
1451 else if (yy == IDENTIFIER || yy == TYPENAME)
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);
1462 else
1463 fprintf (stderr, "->%d <%s>\n", lineno, debug_yytranslate (yy));
1466 #endif
1468 #define NAME(TYPE) cpp_type2name (TYPE)
1470 void
1471 yyerror (msgid)
1472 const char *msgid;
1474 const char *string = _(msgid);
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)
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);
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)
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);
1501 else
1502 error ("%s before `%s' token", string, NAME (last_token));