1 /* Type Analyzer for GNU C++.
2 Copyright (C) 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000 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)
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. */
45 #define SPEW_INLINE inline
49 extern cpp_reader parse_in;
52 /* This takes a token stream that hasn't decided much about types and
53 tries to figure out as much as it can, with excessive lookahead and
56 /* fifo of tokens recognized and available to parser. */
59 /* The values for YYCHAR will fit in a short. */
65 /* Since inline methods can refer to text which has not yet been seen,
66 we store the text of the method in a structure which is placed in the
67 DECL_PENDING_INLINE_INFO field of the FUNCTION_DECL.
68 After parsing the body of the class definition, the FUNCTION_DECL's are
69 scanned to see which ones have this field set. Those are then digested
72 This function's FUNCTION_DECL will have a bit set in its common so
73 that we know to watch out for it. */
77 struct unparsed_text *next; /* process this one next */
78 tree decl; /* associated declaration */
79 const char *filename; /* name of file we were processing */
80 int lineno; /* line number we got the text from */
81 int interface; /* remembering interface_unknown and interface_only */
83 struct token *pos; /* current position, when rescanning */
84 struct token *limit; /* end of saved text */
87 /* Stack of state saved off when we return to an inline method or
88 default argument that has been stored for later parsing. */
91 struct unparsed_text *input;
97 struct obstack token_obstack;
101 static struct obstack feed_obstack;
102 static struct feed *feed;
104 static SPEW_INLINE void do_aggr PARAMS ((void));
105 static SPEW_INLINE int identifier_type PARAMS ((tree));
106 static void scan_tokens PARAMS ((int));
107 static void feed_defarg PARAMS ((tree));
108 static void finish_defarg PARAMS ((void));
109 static int read_token PARAMS ((struct token *));
111 static SPEW_INLINE int num_tokens PARAMS ((void));
112 static SPEW_INLINE struct token *nth_token PARAMS ((int));
113 static SPEW_INLINE int add_token PARAMS ((struct token *));
114 static SPEW_INLINE int shift_token PARAMS ((void));
115 static SPEW_INLINE void push_token PARAMS ((struct token *));
116 static SPEW_INLINE void consume_token PARAMS ((void));
117 static SPEW_INLINE int read_process_identifier PARAMS ((YYSTYPE *));
119 static SPEW_INLINE void feed_input PARAMS ((struct unparsed_text *));
120 static SPEW_INLINE void end_input PARAMS ((void));
121 static SPEW_INLINE void snarf_block PARAMS ((const char *, int));
122 static tree snarf_defarg PARAMS ((void));
123 static int frob_id PARAMS ((int, int, tree *));
125 /* The list of inline functions being held off until we reach the end of
126 the current class declaration. */
127 struct unparsed_text *pending_inlines;
128 struct unparsed_text *pending_inlines_tail;
130 /* The list of previously-deferred inline functions currently being parsed.
131 This exists solely to be a GC root. */
132 struct unparsed_text *processing_these_inlines;
134 static void begin_parsing_inclass_inline PARAMS ((struct unparsed_text *));
135 static void mark_pending_inlines PARAMS ((PTR));
139 static unsigned int yylex_ctr = 0;
141 static void debug_yychar PARAMS ((int));
144 extern char *debug_yytranslate PARAMS ((int));
146 static enum cpp_ttype last_token;
149 /* the declaration found for the last IDENTIFIER token read in.
150 yylex must look this up to detect typedefs, which get token type TYPENAME,
151 so it is left around in case the identifier is not a typedef but is
152 used in a context which makes it a reference to a variable. */
153 extern tree lastiddecl; /* let our brains leak out here too */
154 extern int yychar; /* the lookahead symbol */
155 extern YYSTYPE yylval; /* the semantic value of the */
156 /* lookahead symbol */
157 /* The token fifo lives in this obstack. */
158 struct obstack token_obstack;
161 /* Sometimes we need to save tokens for later parsing. If so, they are
162 stored on this obstack. */
163 struct obstack inline_text_obstack;
164 char *inline_text_firstobj;
166 /* When we see a default argument in a method declaration, we snarf it as
167 text using snarf_defarg. When we get up to namespace scope, we then go
168 through and parse all of them using do_pending_defargs. Since yacc
169 parsers are not reentrant, we retain defargs state in these two
170 variables so that subsequent calls to do_pending_defargs can resume
171 where the previous call left off. */
173 static tree defarg_fns;
174 static tree defarg_parm;
176 /* Initialize obstacks. Called once, from init_parse. */
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);
188 ggc_add_root (&pending_inlines, 1, sizeof (struct unparsed_text *),
189 mark_pending_inlines);
190 ggc_add_root (&processing_these_inlines, 1, sizeof (struct unparsed_text *),
191 mark_pending_inlines);
195 clear_inline_text_obstack ()
197 obstack_free (&inline_text_obstack, inline_text_firstobj);
200 /* Subroutine of read_token. */
201 static SPEW_INLINE int
202 read_process_identifier (pyylval)
205 tree id = pyylval->ttype;
207 if (C_IS_RESERVED_WORD (id))
209 /* Possibly replace the IDENTIFIER_NODE with a magic cookie.
210 Can't put yylval.code numbers in ridpointers[]. Bleah. */
212 switch (C_RID_CODE (id))
214 case RID_BITAND: pyylval->code = BIT_AND_EXPR; return '&';
215 case RID_AND_EQ: pyylval->code = BIT_AND_EXPR; return ASSIGN;
216 case RID_BITOR: pyylval->code = BIT_IOR_EXPR; return '|';
217 case RID_OR_EQ: pyylval->code = BIT_IOR_EXPR; return ASSIGN;
218 case RID_XOR: pyylval->code = BIT_XOR_EXPR; return '^';
219 case RID_XOR_EQ: pyylval->code = BIT_XOR_EXPR; return ASSIGN;
220 case RID_NOT_EQ: pyylval->code = NE_EXPR; return EQCOMPARE;
223 if (C_RID_YYCODE (id) == TYPESPEC)
224 GNU_xref_ref (current_function_decl, IDENTIFIER_POINTER (id));
226 pyylval->ttype = ridpointers[C_RID_CODE (id)];
227 return C_RID_YYCODE (id);
231 GNU_xref_ref (current_function_decl, IDENTIFIER_POINTER (id));
233 /* Make sure that user does not collide with our internal naming
234 scheme. This is not necessary if '.' is used to remove them from
235 the user's namespace, but is if '$' or double underscores are. */
237 #if !defined(JOINER) || JOINER == '$'
240 || DESTRUCTOR_NAME_P (id)
241 || VTABLE_NAME_P (id)
243 || ANON_AGGRNAME_P (id)
244 || ANON_PARMNAME_P (id))
246 "identifier name `%s' conflicts with GNU C++ internal naming strategy",
247 IDENTIFIER_POINTER (id));
252 /* Read the next token from the input file. The token is written into
253 T, and its type number is returned. */
260 last_token = c_lex (&t->yylval.ttype);
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);
331 cpp_pop_buffer (&parse_in);
332 if (CPP_BUFFER (&parse_in))
339 t->yychar = read_process_identifier (&t->yylval);
347 t->yychar = CONSTANT;
355 /* This token should not be generated in C++ mode. */
358 /* These tokens should not survive translation phase 4. */
361 error ("syntax error before '#' token");
372 static SPEW_INLINE void
374 struct unparsed_text *input;
382 f = obstack_alloc (&feed_obstack, sizeof (struct feed));
384 /* The token list starts just after the struct unparsed_text in memory. */
385 input->pos = (struct token *) (input + 1);
389 fprintf (stderr, "\tfeeding %s:%d [%d tokens]\n",
390 input->filename, input->lineno, input->limit - input->pos);
394 f->filename = input_filename;
398 f->first_token = first_token;
399 f->token_obstack = token_obstack;
402 input_filename = input->filename;
403 lineno = input->lineno;
405 yylval.ttype = NULL_TREE;
407 gcc_obstack_init (&token_obstack);
411 static SPEW_INLINE void
414 struct feed *f = feed;
416 input_filename = f->filename;
420 first_token = f->first_token;
421 obstack_free (&token_obstack, 0);
422 token_obstack = f->token_obstack;
425 obstack_free (&feed_obstack, f);
429 fprintf (stderr, "\treturning to %s:%d\n", input_filename, lineno);
433 /* GC callback to mark memory pointed to by the pending inline queue. */
435 mark_pending_inlines (pi)
438 struct unparsed_text *up = * (struct unparsed_text **)pi;
442 struct token *t = (struct token *) (up + 1);
443 struct token *l = up->limit;
447 /* Some of the possible values for yychar use yylval.code
448 instead of yylval.ttype. We only have to worry about
449 yychars that could have been returned by read_token. */
452 case '+': case '-': case '*': case '/':
453 case '%': case '&': case '|': case '^':
454 case '>': case '<': case LSHIFT: case RSHIFT:
455 case ASSIGN: case MIN_MAX: case EQCOMPARE: case ARITHCOMPARE:
460 ggc_mark_tree (t->yylval.ttype);
467 /* Token queue management. */
469 /* Return the number of tokens available on the fifo. */
470 static SPEW_INLINE int
473 return (obstack_object_size (&token_obstack) / sizeof (struct token))
477 /* Fetch the token N down the line from the head of the fifo. */
479 static SPEW_INLINE struct token*
483 #ifdef ENABLE_CHECKING
484 /* could just have this do slurp_ implicitly, but this way is easier
486 my_friendly_assert (n >= 0 && n < num_tokens (), 298);
488 return ((struct token*)obstack_base (&token_obstack)) + n + first_token;
491 static const struct token Teosi = { END_OF_SAVED_INPUT, 0 UNION_INIT_ZERO };
492 static const struct token Tpad = { EMPTY, 0 UNION_INIT_ZERO };
494 /* Copy the next token into T and return its value. */
495 static SPEW_INLINE int
500 return read_token (t);
502 if (feed->input->pos < feed->input->limit)
504 memcpy (t, feed->input->pos, sizeof (struct token));
505 return (feed->input->pos++)->yychar;
508 memcpy (t, &Teosi, sizeof (struct token));
509 return END_OF_SAVED_INPUT;
512 /* Shift the next token onto the fifo. */
513 static SPEW_INLINE int
516 size_t point = obstack_object_size (&token_obstack);
517 obstack_blank (&token_obstack, sizeof (struct token));
518 return add_token ((struct token *) (obstack_base (&token_obstack) + point));
521 /* Consume the next token out of the fifo. */
523 static SPEW_INLINE void
526 if (num_tokens () == 1)
528 obstack_free (&token_obstack, obstack_base (&token_obstack));
535 /* Push a token at the head of the queue; it will be the next token read. */
536 static SPEW_INLINE void
540 if (first_token == 0) /* We hope this doesn't happen often. */
542 size_t active = obstack_object_size (&token_obstack);
543 obstack_blank (&token_obstack, sizeof (struct token));
545 memmove (obstack_base (&token_obstack) + sizeof (struct token),
546 obstack_base (&token_obstack), active);
550 memcpy (nth_token (0), t, sizeof (struct token));
554 /* Pull in enough tokens that the queue is N long beyond the current
562 int num = num_tokens ();
565 /* First, prune any empty tokens at the end. */
567 while (i > 0 && nth_token (i - 1)->yychar == EMPTY)
571 obstack_blank (&token_obstack, -((num - i) * sizeof (struct token)));
575 /* Now, if we already have enough tokens, return. */
579 /* Never read past these characters: they might separate
580 the current input stream from one we save away later. */
581 for (i = 0; i < num; i++)
583 yychar = nth_token (i)->yychar;
584 if (yychar == '{' || yychar == ':' || yychar == ';')
588 while (num_tokens () <= n)
590 yychar = shift_token ();
591 if (yychar == '{' || yychar == ':' || yychar == ';')
597 while (num_tokens () <= n)
598 obstack_grow (&token_obstack, &Tpad, sizeof (struct token));
601 int looking_for_typename;
602 int looking_for_template;
604 static int after_friend;
605 static int after_new;
606 static int do_snarf_defarg;
611 static SPEW_INLINE int
612 identifier_type (decl)
617 if (TREE_CODE (decl) == TEMPLATE_DECL)
619 if (TREE_CODE (DECL_TEMPLATE_RESULT (decl)) == TYPE_DECL)
621 else if (looking_for_template)
624 if (looking_for_template && really_overloaded_fn (decl))
626 /* See through a baselink. */
627 if (TREE_CODE (decl) == TREE_LIST)
628 decl = TREE_VALUE (decl);
630 for (t = decl; t != NULL_TREE; t = OVL_CHAIN (t))
631 if (DECL_FUNCTION_TEMPLATE_P (OVL_FUNCTION (t)))
634 if (TREE_CODE (decl) == NAMESPACE_DECL)
636 if (TREE_CODE (decl) != TYPE_DECL)
638 if (DECL_ARTIFICIAL (decl) && TREE_TYPE (decl) == current_class_type)
641 /* A constructor declarator for a template type will get here as an
642 implicit typename, a TYPENAME_TYPE with a type. */
644 if (t && TREE_CODE (t) == TYPENAME_TYPE)
646 decl = TREE_TYPE (decl);
647 if (TREE_CODE (decl) == TYPENAME_TYPE)
648 decl = TREE_TYPE (decl);
655 /* token[0] == AGGR (struct/union/enum)
656 Thus, token[1] is either a TYPENAME or a TYPENAME_DEFN.
657 If token[2] == '{' or ':' then it's TYPENAME_DEFN.
658 It's also a definition if it's a forward declaration (as in 'struct Foo;')
659 which we can tell if token[2] == ';' *and* token[-1] != FRIEND or NEW. */
661 static SPEW_INLINE void
667 yc1 = nth_token (1)->yychar;
668 if (yc1 != TYPENAME && yc1 != IDENTIFIER && yc1 != PTYPENAME)
670 yc2 = nth_token (2)->yychar;
673 /* It's a forward declaration iff we were not preceded by
674 'friend' or `new'. */
675 if (after_friend || after_new)
678 else if (yc2 != '{' && yc2 != ':')
684 nth_token (1)->yychar = TYPENAME_DEFN;
687 nth_token (1)->yychar = PTYPENAME_DEFN;
690 nth_token (1)->yychar = IDENTIFIER_DEFN;
693 my_friendly_abort (102);
700 /* Only types expected, not even namespaces. */
701 looking_for_typename = 2;
703 if ((yychar = yylex ()) < 0) yychar = 0;
704 looking_for_typename = 0;
705 if (yychar == IDENTIFIER)
707 lastiddecl = lookup_name (yylval.ttype, -2);
711 lastiddecl = IDENTIFIER_LABEL_VALUE (yylval.ttype);
714 yychar = identifier_type (lastiddecl);
722 int old_looking_for_typename = 0;
724 timevar_push (TV_LEX);
731 fprintf (stderr, "\t\t## %d @%d ", yylex_ctr, lineno);
738 yylval.ttype = snarf_defarg ();
740 got_object = NULL_TREE;
741 timevar_pop (TV_LEX);
745 /* if we've got tokens, send them */
746 else if (num_tokens ())
747 yychr = nth_token (0)->yychar;
749 yychr = shift_token ();
751 /* many tokens just need to be returned. At first glance, all we
752 have to do is send them back up, but some of them are needed to
753 figure out local context. */
757 /* This is a lexical no-op. */
760 debug_yychar (yychr);
767 if (nth_token (1)->yychar == ')')
779 peek = nth_token (1)->yychar;
780 yychr = frob_id (yychr, peek, &nth_token (0)->yylval.ttype);
783 case IDENTIFIER_DEFN:
788 /* If we see a SCOPE next, restore the old value.
789 Otherwise, we got what we want. */
790 looking_for_typename = old_looking_for_typename;
791 looking_for_template = 0;
795 if (nth_token (0)->yylval.ttype == ridpointers[RID_EXTERN])
798 if (nth_token (1)->yychar == STRING)
800 yychr = EXTERN_LANG_STRING;
801 nth_token (1)->yylval.ttype = get_identifier
802 (TREE_STRING_POINTER (nth_token (1)->yylval.ttype));
806 /* If export, warn that it's unimplemented and go on. */
807 else if (nth_token (0)->yylval.ttype == ridpointers[RID_EXPORT])
809 warning ("keyword 'export' not implemented and will be ignored");
812 debug_yychar (yychr);
817 /* do_aggr needs to know if the previous token was `friend'. */
818 else if (nth_token (0)->yylval.ttype == ridpointers[RID_FRIEND])
824 /* do_aggr needs to know if the previous token was `new'. */
832 /* If this provides a type for us, then revert lexical
833 state to standard state. */
834 looking_for_typename = 0;
839 after_friend = after_new = 0;
843 /* Set this again, in case we are rescanning. */
844 looking_for_typename = 2;
851 /* class member lookup only applies to the first token after the object
852 expression, except for explicit destructor calls. */
854 got_object = NULL_TREE;
857 yylval = nth_token (0)->yylval;
858 lineno = nth_token (0)->lineno;
862 debug_yychar (yychr);
866 timevar_pop (TV_LEX);
870 /* Unget character CH from the input stream.
871 If RESCAN is non-zero, then we want to `see' this
872 character as the next input token. */
875 yyungetc (ch, rescan)
879 /* Unget a character from the input stream. */
880 if (yychar == YYEMPTY || rescan == 0)
884 /* If we're putting back a brace, undo the change in indent_level
885 from the first time we saw it. */
892 fake.yylval.ttype = 0;
893 fake.lineno = lineno;
903 /* Lexer hackery to determine what *IDP really is. */
906 frob_id (yyc, peek, idp)
912 int old_looking_for_typename = 0;
916 /* Don't interfere with the setting from an 'aggr' prefix. */
917 old_looking_for_typename = looking_for_typename;
918 looking_for_typename = 1;
920 else if (peek == '<')
921 looking_for_template = 1;
922 trrr = lookup_name (*idp, -2);
925 yyc = identifier_type (trrr);
932 /* If this got special lookup, remember it. In these
933 cases, we know it can't be a declarator-id. */
934 if (got_scope || got_object)
942 my_friendly_abort (20000907);
946 lastiddecl = NULL_TREE;
947 got_scope = NULL_TREE;
948 looking_for_typename = old_looking_for_typename;
949 looking_for_template = 0;
953 /* ID is an operator name. Duplicate the hackery in yylex to determine what
956 tree frob_opname (id)
960 frob_id (0, nth_token (0)->yychar, &id);
961 got_object = NULL_TREE;
965 /* Set up the state required to correctly handle the definition of the
966 inline function whose preparsed state has been saved in PI. */
969 begin_parsing_inclass_inline (pi)
970 struct unparsed_text *pi;
974 /* Record that we are processing the chain of inlines starting at
975 PI in a special GC root. */
976 processing_these_inlines = pi;
980 /* If this is an inline function in a local class, we must make sure
981 that we save all pertinent information about the function
982 surrounding the local class. */
983 context = decl_function_context (pi->decl);
985 push_function_context_to (context);
988 interface_unknown = pi->interface == 1;
989 interface_only = pi->interface == 0;
990 DECL_PENDING_INLINE_P (pi->decl) = 0;
991 DECL_PENDING_INLINE_INFO (pi->decl) = 0;
993 /* Pass back a handle to the rest of the inline functions, so that they
994 can be processed later. */
995 yychar = PRE_PARSED_FUNCTION_DECL;
998 start_function (NULL_TREE, pi->decl, NULL_TREE,
999 (SF_DEFAULT | SF_PRE_PARSED | SF_INCLASS_INLINE));
1002 /* Called from the top level: if there are any pending inlines to
1003 do, set up to process them now. This function sets up the first function
1004 to be parsed; after it has been, the rule for fndef in parse.y will
1005 call process_next_inline to start working on the next one. */
1008 do_pending_inlines ()
1010 /* Oops, we're still dealing with the last batch. */
1011 if (yychar == PRE_PARSED_FUNCTION_DECL)
1014 if (pending_inlines)
1016 /* Clear the chain, so that any inlines nested inside the batch
1017 we're to process now don't refer to this batch. See e.g.
1018 g++.other/lookup6.C. */
1019 struct unparsed_text *first = pending_inlines;
1020 pending_inlines = pending_inlines_tail = 0;
1022 begin_parsing_inclass_inline (first);
1026 /* Called from the fndecl rule in the parser when the function just parsed
1027 was declared using a PRE_PARSED_FUNCTION_DECL (i.e. came from
1028 do_pending_inlines). */
1031 process_next_inline (i)
1032 struct unparsed_text *i;
1034 tree decl = i->decl;
1035 tree context = decl_function_context (decl);
1038 pop_function_context_from (context);
1039 if (yychar == YYEMPTY)
1041 if (yychar != END_OF_SAVED_INPUT)
1042 error ("parse error at end of saved function text");
1047 begin_parsing_inclass_inline (i);
1050 processing_these_inlines = 0;
1051 extract_interface_info ();
1056 /* Subroutine of snarf_method, deals with actual absorption of the block. */
1058 static SPEW_INLINE void
1059 snarf_block (starting_file, starting_line)
1060 const char *starting_file;
1064 int look_for_semicolon = 0;
1065 int look_for_lbrac = 0;
1066 int look_for_catch = 0;
1072 /* We incremented indent_level in yylex; undo that. */
1074 else if (yychar == '=')
1075 look_for_semicolon = 1;
1076 else if (yychar == ':' || yychar == RETURN_KEYWORD || yychar == TRY)
1084 yyerror ("parse error in method specification");
1086 /* The current token is the first one to be recorded. */
1087 tmp.yychar = yychar;
1088 tmp.yylval = yylval;
1089 tmp.lineno = lineno;
1090 obstack_grow (&inline_text_obstack, &tmp, sizeof (struct token));
1094 point = obstack_object_size (&inline_text_obstack);
1095 obstack_blank (&inline_text_obstack, sizeof (struct token));
1096 yyc = add_token ((struct token *)
1097 (obstack_base (&inline_text_obstack) + point));
1104 else if (yyc == '}')
1107 if (blev == 0 && !look_for_semicolon)
1109 if (!look_for_catch)
1112 if (add_token (&tmp) != CATCH)
1119 obstack_grow (&inline_text_obstack, &tmp, sizeof (struct token));
1122 else if (yyc == ';')
1126 error ("function body for constructor missing");
1127 /* fake a { } to avoid further errors */
1128 tmp.yylval.ttype = 0;
1130 obstack_grow (&inline_text_obstack, &tmp, sizeof (struct token));
1132 obstack_grow (&inline_text_obstack, &tmp, sizeof (struct token));
1135 else if (look_for_semicolon && blev == 0)
1140 error_with_file_and_line (starting_file, starting_line,
1141 "end of file read inside definition");
1147 /* This function stores away the text for an inline function that should
1148 be processed later (by do_pending_inlines). */
1153 int starting_lineno = lineno;
1154 const char *starting_filename = input_filename;
1157 struct unparsed_text *meth;
1159 /* Leave room for the header, then absorb the block. */
1160 obstack_blank (&inline_text_obstack, sizeof (struct unparsed_text));
1161 snarf_block (starting_filename, starting_lineno);
1163 len = obstack_object_size (&inline_text_obstack);
1164 meth = (struct unparsed_text *) obstack_finish (&inline_text_obstack);
1166 /* Happens when we get two declarations of the same function in the
1168 if (decl == void_type_node
1169 || (current_class_type && TYPE_REDEFINED (current_class_type)))
1171 obstack_free (&inline_text_obstack, (char *)meth);
1176 meth->filename = starting_filename;
1177 meth->lineno = starting_lineno;
1178 meth->limit = (struct token *) ((char *)meth + len);
1179 meth->interface = (interface_unknown ? 1 : (interface_only ? 0 : 2));
1184 fprintf (stderr, "\tsaved method of %d tokens from %s:%d\n",
1185 meth->limit - (struct token *) (meth + 1),
1186 starting_filename, starting_lineno);
1189 DECL_PENDING_INLINE_INFO (decl) = meth;
1190 DECL_PENDING_INLINE_P (decl) = 1;
1192 if (pending_inlines_tail)
1193 pending_inlines_tail->next = meth;
1195 pending_inlines = meth;
1196 pending_inlines_tail = meth;
1199 /* Consume a no-commas expression - a default argument - and save it
1200 on the inline_text_obstack. */
1205 int starting_lineno = lineno;
1206 const char *starting_filename = input_filename;
1211 struct unparsed_text *buf;
1214 obstack_blank (&inline_text_obstack, sizeof (struct unparsed_text));
1218 point = obstack_object_size (&inline_text_obstack);
1219 obstack_blank (&inline_text_obstack, sizeof (struct token));
1220 yyc = add_token ((struct token *)
1221 (obstack_base (&inline_text_obstack) + point));
1223 if (plev <= 0 && (yyc == ')' || yyc == ','))
1225 else if (yyc == '(' || yyc == '[')
1227 else if (yyc == ']' || yyc == ')')
1231 error_with_file_and_line (starting_filename, starting_lineno,
1232 "end of file read inside default argument");
1237 /* Unget the last token. */
1238 push_token ((struct token *) (obstack_base (&inline_text_obstack) + point));
1239 /* This is the documented way to shrink a growing obstack block. */
1240 obstack_blank (&inline_text_obstack, - (int) sizeof (struct token));
1243 len = obstack_object_size (&inline_text_obstack);
1244 buf = (struct unparsed_text *) obstack_finish (&inline_text_obstack);
1247 buf->filename = starting_filename;
1248 buf->lineno = starting_lineno;
1249 buf->limit = (struct token *) ((char *)buf + len);
1254 fprintf (stderr, "\tsaved defarg of %d tokens from %s:%d\n",
1255 buf->limit - (struct token *) (buf + 1),
1256 starting_filename, starting_lineno);
1259 arg = make_node (DEFAULT_ARG);
1260 DEFARG_POINTER (arg) = (char *)buf;
1265 /* Decide whether the default argument we are about to see should be
1266 gobbled up as text for later parsing. */
1269 maybe_snarf_defarg ()
1271 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
1272 do_snarf_defarg = 1;
1275 /* Called from grokfndecl to note a function decl with unparsed default
1276 arguments for later processing. Also called from grokdeclarator
1277 for function types with unparsed defargs; the call from grokfndecl
1278 will always come second, so we can overwrite the entry from the type. */
1281 add_defarg_fn (decl)
1284 if (TREE_CODE (decl) == FUNCTION_DECL)
1285 TREE_VALUE (defarg_fns) = decl;
1287 defarg_fns = tree_cons (current_class_type, decl, defarg_fns);
1290 /* Helper for do_pending_defargs. Starts the parsing of a default arg. */
1296 tree d = TREE_PURPOSE (p);
1298 feed_input ((struct unparsed_text *)DEFARG_POINTER (d));
1299 yychar = DEFARG_MARKER;
1303 /* Helper for do_pending_defargs. Ends the parsing of a default arg. */
1308 if (yychar == YYEMPTY)
1310 if (yychar != END_OF_SAVED_INPUT)
1311 error ("parse error at end of saved function text");
1316 /* Main function for deferred parsing of default arguments. Called from
1320 do_pending_defargs ()
1325 for (; defarg_fns; defarg_fns = TREE_CHAIN (defarg_fns))
1327 tree defarg_fn = TREE_VALUE (defarg_fns);
1328 if (defarg_parm == NULL_TREE)
1330 push_nested_class (TREE_PURPOSE (defarg_fns), 1);
1332 if (TREE_CODE (defarg_fn) == FUNCTION_DECL)
1333 maybe_begin_member_template_processing (defarg_fn);
1335 if (TREE_CODE (defarg_fn) == FUNCTION_DECL)
1336 defarg_parm = TYPE_ARG_TYPES (TREE_TYPE (defarg_fn));
1338 defarg_parm = TYPE_ARG_TYPES (defarg_fn);
1341 defarg_parm = TREE_CHAIN (defarg_parm);
1343 for (; defarg_parm; defarg_parm = TREE_CHAIN (defarg_parm))
1344 if (TREE_PURPOSE (defarg_parm)
1345 && TREE_CODE (TREE_PURPOSE (defarg_parm)) == DEFAULT_ARG)
1347 feed_defarg (defarg_parm);
1349 /* Return to the parser, which will process this defarg
1350 and call us again. */
1354 if (TREE_CODE (defarg_fn) == FUNCTION_DECL)
1356 maybe_end_member_template_processing ();
1357 check_default_args (defarg_fn);
1361 pop_nested_class ();
1366 /* debug_yychar takes a yychar (token number) value and prints its name. */
1373 fprintf (stderr, "->%d < %c >\n", lineno, yy);
1374 else if (yy == IDENTIFIER || yy == TYPENAME)
1377 if (TREE_CODE (yylval.ttype) == IDENTIFIER_NODE)
1378 id = IDENTIFIER_POINTER (yylval.ttype);
1379 else if (TREE_CODE_CLASS (TREE_CODE (yylval.ttype)) == 'd')
1380 id = IDENTIFIER_POINTER (DECL_NAME (yylval.ttype));
1383 fprintf (stderr, "->%d <%s `%s'>\n", lineno, debug_yytranslate (yy), id);
1386 fprintf (stderr, "->%d <%s>\n", lineno, debug_yytranslate (yy));
1392 #define NAME(type) cpp_type2name (type)
1397 #define TK(e, s) STRINGX(e),
1399 static const char *type2name[N_TTYPES] = { TTYPE_TABLE };
1400 #define NAME(type) type2name[type]
1407 const char *string = _(msgid);
1409 if (last_token == CPP_EOF)
1410 error ("%s at end of input", string);
1411 else if (last_token == CPP_CHAR || last_token == CPP_WCHAR)
1413 unsigned int val = TREE_INT_CST_LOW (yylval.ttype);
1414 const char *ell = (last_token == CPP_CHAR) ? "" : "L";
1415 if (val <= UCHAR_MAX && ISGRAPH (val))
1416 error ("%s before %s'%c'", string, ell, val);
1418 error ("%s before %s'\\x%x'", string, ell, val);
1420 else if (last_token == CPP_STRING
1421 || last_token == CPP_WSTRING
1422 || last_token == CPP_OSTRING)
1423 error ("%s before string constant", string);
1424 else if (last_token == CPP_NUMBER
1425 || last_token == CPP_INT
1426 || last_token == CPP_FLOAT)
1427 error ("%s before numeric constant", string);
1428 else if (last_token == CPP_NAME
1429 && TREE_CODE (yylval.ttype) == IDENTIFIER_NODE)
1430 error ("%s before \"%s\"", string, IDENTIFIER_POINTER (yylval.ttype));
1432 error ("%s before '%s' token", string, NAME(last_token));