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
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
52 /* fifo of tokens recognized and available to parser. */
55 /* The values for YYCHAR will fit in a short. */
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
68 This function's FUNCTION_DECL will have a bit set in its common so
69 that we know to watch out for it. */
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. */
87 struct unparsed_text *input;
93 struct obstack token_obstack;
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 end_input PARAMS ((void));
117 static SPEW_INLINE void snarf_block PARAMS ((const char *, int));
118 static tree snarf_defarg PARAMS ((void));
119 static int frob_id PARAMS ((int, int, tree *));
121 /* The list of inline functions being held off until we reach the end of
122 the current class declaration. */
123 struct unparsed_text *pending_inlines;
124 struct unparsed_text *pending_inlines_tail;
126 /* The list of previously-deferred inline functions currently being parsed.
127 This exists solely to be a GC root. */
128 struct unparsed_text *processing_these_inlines;
130 static void begin_parsing_inclass_inline PARAMS ((struct unparsed_text *));
131 static void mark_pending_inlines PARAMS ((PTR));
135 static unsigned int yylex_ctr = 0;
137 static void debug_yychar PARAMS ((int));
140 extern char *debug_yytranslate PARAMS ((int));
142 static enum cpp_ttype last_token;
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;
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 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);
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);
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)
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;
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 == '$'
242 || DESTRUCTOR_NAME_P (id)
243 || VTABLE_NAME_P (id)
245 || ANON_AGGRNAME_P (id)
246 || ANON_PARMNAME_P (id))
248 "identifier name `%s' conflicts with GNU C++ internal naming strategy",
249 IDENTIFIER_POINTER (id));
254 /* Read the next token from the input file. The token is written into
255 T, and its type number is returned. */
262 last_token = c_lex (&t->yylval.ttype);
266 #define YYCHAR(yy) t->yychar = yy; break;
267 #define YYCODE(c) t->yylval.code = c;
269 case CPP_EQ: YYCHAR('=');
270 case CPP_NOT: YYCHAR('!');
271 case CPP_GREATER: YYCODE(GT_EXPR); YYCHAR('>');
272 case CPP_LESS: YYCODE(LT_EXPR); YYCHAR('<');
273 case CPP_PLUS: YYCODE(PLUS_EXPR); YYCHAR('+');
274 case CPP_MINUS: YYCODE(MINUS_EXPR); YYCHAR('-');
275 case CPP_MULT: YYCODE(MULT_EXPR); YYCHAR('*');
276 case CPP_DIV: YYCODE(TRUNC_DIV_EXPR); YYCHAR('/');
277 case CPP_MOD: YYCODE(TRUNC_MOD_EXPR); YYCHAR('%');
278 case CPP_AND: YYCODE(BIT_AND_EXPR); YYCHAR('&');
279 case CPP_OR: YYCODE(BIT_IOR_EXPR); YYCHAR('|');
280 case CPP_XOR: YYCODE(BIT_XOR_EXPR); YYCHAR('^');
281 case CPP_RSHIFT: YYCODE(RSHIFT_EXPR); YYCHAR(RSHIFT);
282 case CPP_LSHIFT: YYCODE(LSHIFT_EXPR); YYCHAR(LSHIFT);
284 case CPP_COMPL: YYCHAR('~');
285 case CPP_AND_AND: YYCHAR(ANDAND);
286 case CPP_OR_OR: YYCHAR(OROR);
287 case CPP_QUERY: YYCHAR('?');
288 case CPP_COLON: YYCHAR(':');
289 case CPP_COMMA: YYCHAR(',');
290 case CPP_OPEN_PAREN: YYCHAR('(');
291 case CPP_CLOSE_PAREN: YYCHAR(')');
292 case CPP_EQ_EQ: YYCODE(EQ_EXPR); YYCHAR(EQCOMPARE);
293 case CPP_NOT_EQ: YYCODE(NE_EXPR); YYCHAR(EQCOMPARE);
294 case CPP_GREATER_EQ:YYCODE(GE_EXPR); YYCHAR(ARITHCOMPARE);
295 case CPP_LESS_EQ: YYCODE(LE_EXPR); YYCHAR(ARITHCOMPARE);
297 case CPP_PLUS_EQ: YYCODE(PLUS_EXPR); YYCHAR(ASSIGN);
298 case CPP_MINUS_EQ: YYCODE(MINUS_EXPR); YYCHAR(ASSIGN);
299 case CPP_MULT_EQ: YYCODE(MULT_EXPR); YYCHAR(ASSIGN);
300 case CPP_DIV_EQ: YYCODE(TRUNC_DIV_EXPR); YYCHAR(ASSIGN);
301 case CPP_MOD_EQ: YYCODE(TRUNC_MOD_EXPR); YYCHAR(ASSIGN);
302 case CPP_AND_EQ: YYCODE(BIT_AND_EXPR); YYCHAR(ASSIGN);
303 case CPP_OR_EQ: YYCODE(BIT_IOR_EXPR); YYCHAR(ASSIGN);
304 case CPP_XOR_EQ: YYCODE(BIT_XOR_EXPR); YYCHAR(ASSIGN);
305 case CPP_RSHIFT_EQ: YYCODE(RSHIFT_EXPR); YYCHAR(ASSIGN);
306 case CPP_LSHIFT_EQ: YYCODE(LSHIFT_EXPR); YYCHAR(ASSIGN);
308 case CPP_OPEN_SQUARE: YYCHAR('[');
309 case CPP_CLOSE_SQUARE: YYCHAR(']');
310 case CPP_OPEN_BRACE: YYCHAR('{');
311 case CPP_CLOSE_BRACE: YYCHAR('}');
312 case CPP_SEMICOLON: YYCHAR(';');
313 case CPP_ELLIPSIS: YYCHAR(ELLIPSIS);
315 case CPP_PLUS_PLUS: YYCHAR(PLUSPLUS);
316 case CPP_MINUS_MINUS: YYCHAR(MINUSMINUS);
317 case CPP_DEREF: YYCHAR(POINTSAT);
318 case CPP_DOT: YYCHAR('.');
320 /* These tokens are C++ specific. */
321 case CPP_SCOPE: YYCHAR(SCOPE);
322 case CPP_DEREF_STAR: YYCHAR(POINTSAT_STAR);
323 case CPP_DOT_STAR: YYCHAR(DOT_STAR);
324 case CPP_MIN_EQ: YYCODE(MIN_EXPR); YYCHAR(ASSIGN);
325 case CPP_MAX_EQ: YYCODE(MAX_EXPR); YYCHAR(ASSIGN);
326 case CPP_MIN: YYCODE(MIN_EXPR); YYCHAR(MIN_MAX);
327 case CPP_MAX: YYCODE(MAX_EXPR); YYCHAR(MIN_MAX);
332 if (cpp_pop_buffer (parse_in) != 0)
338 t->yychar = read_process_identifier (&t->yylval);
346 t->yychar = CONSTANT;
354 /* These tokens should not survive translation phase 4. */
357 error ("syntax error before '#' token");
368 static SPEW_INLINE void
370 struct unparsed_text *input;
378 f = obstack_alloc (&feed_obstack, sizeof (struct feed));
380 /* The token list starts just after the struct unparsed_text in memory. */
381 input->pos = (struct token *) (input + 1);
385 fprintf (stderr, "\tfeeding %s:%d [%d tokens]\n",
386 input->filename, input->lineno, input->limit - input->pos);
390 f->filename = input_filename;
394 f->first_token = first_token;
395 f->token_obstack = token_obstack;
398 input_filename = input->filename;
399 lineno = input->lineno;
401 yylval.ttype = NULL_TREE;
403 gcc_obstack_init (&token_obstack);
407 static SPEW_INLINE void
410 struct feed *f = feed;
412 input_filename = f->filename;
416 first_token = f->first_token;
417 obstack_free (&token_obstack, 0);
418 token_obstack = f->token_obstack;
421 obstack_free (&feed_obstack, f);
425 fprintf (stderr, "\treturning to %s:%d\n", input_filename, lineno);
429 /* GC callback to mark memory pointed to by the pending inline queue. */
431 mark_pending_inlines (pi)
434 struct unparsed_text *up = * (struct unparsed_text **)pi;
438 struct token *t = (struct token *) (up + 1);
439 struct token *l = up->limit;
443 /* Some of the possible values for yychar use yylval.code
444 instead of yylval.ttype. We only have to worry about
445 yychars that could have been returned by read_token. */
448 case '+': case '-': case '*': case '/':
449 case '%': case '&': case '|': case '^':
450 case '>': case '<': case LSHIFT: case RSHIFT:
451 case ASSIGN: case MIN_MAX: case EQCOMPARE: case ARITHCOMPARE:
456 ggc_mark_tree (t->yylval.ttype);
463 /* Token queue management. */
465 /* Return the number of tokens available on the fifo. */
466 static SPEW_INLINE int
469 return (obstack_object_size (&token_obstack) / sizeof (struct token))
473 /* Fetch the token N down the line from the head of the fifo. */
475 static SPEW_INLINE struct token*
479 #ifdef ENABLE_CHECKING
480 /* could just have this do slurp_ implicitly, but this way is easier
482 my_friendly_assert (n >= 0 && n < num_tokens (), 298);
484 return ((struct token*)obstack_base (&token_obstack)) + n + first_token;
487 static const struct token Teosi = { END_OF_SAVED_INPUT, 0 UNION_INIT_ZERO };
488 static const struct token Tpad = { EMPTY, 0 UNION_INIT_ZERO };
490 /* Copy the next token into T and return its value. */
491 static SPEW_INLINE int
496 return read_token (t);
498 if (feed->input->pos < feed->input->limit)
500 memcpy (t, feed->input->pos, sizeof (struct token));
501 return (feed->input->pos++)->yychar;
504 memcpy (t, &Teosi, sizeof (struct token));
505 return END_OF_SAVED_INPUT;
508 /* Shift the next token onto the fifo. */
509 static SPEW_INLINE int
512 size_t point = obstack_object_size (&token_obstack);
513 obstack_blank (&token_obstack, sizeof (struct token));
514 return add_token ((struct token *) (obstack_base (&token_obstack) + point));
517 /* Consume the next token out of the fifo. */
519 static SPEW_INLINE void
522 if (num_tokens () == 1)
524 obstack_free (&token_obstack, obstack_base (&token_obstack));
531 /* Push a token at the head of the queue; it will be the next token read. */
532 static SPEW_INLINE void
536 if (first_token == 0) /* We hope this doesn't happen often. */
538 size_t active = obstack_object_size (&token_obstack);
539 obstack_blank (&token_obstack, sizeof (struct token));
541 memmove (obstack_base (&token_obstack) + sizeof (struct token),
542 obstack_base (&token_obstack), active);
546 memcpy (nth_token (0), t, sizeof (struct token));
550 /* Pull in enough tokens that the queue is N long beyond the current
558 int num = num_tokens ();
561 /* First, prune any empty tokens at the end. */
563 while (i > 0 && nth_token (i - 1)->yychar == EMPTY)
567 obstack_blank (&token_obstack, -((num - i) * sizeof (struct token)));
571 /* Now, if we already have enough tokens, return. */
575 /* Never read past these characters: they might separate
576 the current input stream from one we save away later. */
577 for (i = 0; i < num; i++)
579 yychar = nth_token (i)->yychar;
580 if (yychar == '{' || yychar == ':' || yychar == ';')
584 while (num_tokens () <= n)
586 yychar = shift_token ();
587 if (yychar == '{' || yychar == ':' || yychar == ';')
593 while (num_tokens () <= n)
594 obstack_grow (&token_obstack, &Tpad, sizeof (struct token));
597 int looking_for_typename;
598 int looking_for_template;
600 static int after_friend;
601 static int after_new;
602 static int do_snarf_defarg;
607 static SPEW_INLINE int
608 identifier_type (decl)
613 if (TREE_CODE (decl) == TEMPLATE_DECL)
615 if (TREE_CODE (DECL_TEMPLATE_RESULT (decl)) == TYPE_DECL)
617 else if (looking_for_template)
620 if (looking_for_template && really_overloaded_fn (decl))
622 /* See through a baselink. */
623 if (TREE_CODE (decl) == TREE_LIST)
624 decl = TREE_VALUE (decl);
626 for (t = decl; t != NULL_TREE; t = OVL_CHAIN (t))
627 if (DECL_FUNCTION_TEMPLATE_P (OVL_FUNCTION (t)))
630 if (TREE_CODE (decl) == NAMESPACE_DECL)
632 if (TREE_CODE (decl) != TYPE_DECL)
634 if (DECL_ARTIFICIAL (decl) && TREE_TYPE (decl) == current_class_type)
637 /* A constructor declarator for a template type will get here as an
638 implicit typename, a TYPENAME_TYPE with a type. */
640 if (t && TREE_CODE (t) == TYPENAME_TYPE)
642 decl = TREE_TYPE (decl);
643 if (TREE_CODE (decl) == TYPENAME_TYPE)
644 decl = TREE_TYPE (decl);
651 /* token[0] == AGGR (struct/union/enum)
652 Thus, token[1] is either a TYPENAME or a TYPENAME_DEFN.
653 If token[2] == '{' or ':' then it's TYPENAME_DEFN.
654 It's also a definition if it's a forward declaration (as in 'struct Foo;')
655 which we can tell if token[2] == ';' *and* token[-1] != FRIEND or NEW. */
657 static SPEW_INLINE void
663 yc1 = nth_token (1)->yychar;
664 if (yc1 != TYPENAME && yc1 != IDENTIFIER && yc1 != PTYPENAME)
666 yc2 = nth_token (2)->yychar;
669 /* It's a forward declaration iff we were not preceded by
670 'friend' or `new'. */
671 if (after_friend || after_new)
674 else if (yc2 != '{' && yc2 != ':')
680 nth_token (1)->yychar = TYPENAME_DEFN;
683 nth_token (1)->yychar = PTYPENAME_DEFN;
686 nth_token (1)->yychar = IDENTIFIER_DEFN;
689 my_friendly_abort (102);
696 /* Only types expected, not even namespaces. */
697 looking_for_typename = 2;
699 if ((yychar = yylex ()) < 0) yychar = 0;
700 looking_for_typename = 0;
701 if (yychar == IDENTIFIER)
703 lastiddecl = lookup_name (yylval.ttype, -2);
707 lastiddecl = IDENTIFIER_LABEL_VALUE (yylval.ttype);
710 yychar = identifier_type (lastiddecl);
718 int old_looking_for_typename = 0;
719 int just_saw_new = 0;
720 int just_saw_friend = 0;
722 timevar_push (TV_LEX);
729 fprintf (stderr, "\t\t## %d @%d ", yylex_ctr, lineno);
736 yylval.ttype = snarf_defarg ();
738 got_object = NULL_TREE;
739 timevar_pop (TV_LEX);
743 /* if we've got tokens, send them */
744 else if (num_tokens ())
745 yychr = nth_token (0)->yychar;
747 yychr = shift_token ();
749 /* many tokens just need to be returned. At first glance, all we
750 have to do is send them back up, but some of them are needed to
751 figure out local context. */
755 /* This is a lexical no-op. */
758 debug_yychar (yychr);
765 if (nth_token (1)->yychar == ')')
777 peek = nth_token (1)->yychar;
778 yychr = frob_id (yychr, peek, &nth_token (0)->yylval.ttype);
781 case IDENTIFIER_DEFN:
786 /* If we see a SCOPE next, restore the old value.
787 Otherwise, we got what we want. */
788 looking_for_typename = old_looking_for_typename;
789 looking_for_template = 0;
793 if (nth_token (0)->yylval.ttype == ridpointers[RID_EXTERN])
796 if (nth_token (1)->yychar == STRING)
798 yychr = EXTERN_LANG_STRING;
799 nth_token (1)->yylval.ttype = get_identifier
800 (TREE_STRING_POINTER (nth_token (1)->yylval.ttype));
804 /* do_aggr needs to know if the previous token was `friend'. */
805 else if (nth_token (0)->yylval.ttype == ridpointers[RID_FRIEND])
811 /* do_aggr needs to know if the previous token was `new'. */
819 /* If this provides a type for us, then revert lexical
820 state to standard state. */
821 looking_for_typename = 0;
829 /* Set this again, in case we are rescanning. */
830 looking_for_typename = 2;
837 after_friend = just_saw_friend;
838 after_new = just_saw_new;
840 /* class member lookup only applies to the first token after the object
841 expression, except for explicit destructor calls. */
843 got_object = NULL_TREE;
846 yylval = nth_token (0)->yylval;
847 lineno = nth_token (0)->lineno;
851 debug_yychar (yychr);
855 timevar_pop (TV_LEX);
859 /* Unget character CH from the input stream.
860 If RESCAN is non-zero, then we want to `see' this
861 character as the next input token. */
864 yyungetc (ch, rescan)
868 /* Unget a character from the input stream. */
869 if (yychar == YYEMPTY || rescan == 0)
873 /* If we're putting back a brace, undo the change in indent_level
874 from the first time we saw it. */
881 fake.yylval.ttype = 0;
882 fake.lineno = lineno;
892 /* Lexer hackery to determine what *IDP really is. */
895 frob_id (yyc, peek, idp)
901 int old_looking_for_typename = 0;
905 /* Don't interfere with the setting from an 'aggr' prefix. */
906 old_looking_for_typename = looking_for_typename;
907 looking_for_typename = 1;
909 else if (peek == '<')
910 looking_for_template = 1;
911 trrr = lookup_name (*idp, -2);
914 yyc = identifier_type (trrr);
921 /* If this got special lookup, remember it. In these
922 cases, we know it can't be a declarator-id. */
923 if (got_scope || got_object)
931 my_friendly_abort (20000907);
935 lastiddecl = NULL_TREE;
936 got_scope = NULL_TREE;
937 looking_for_typename = old_looking_for_typename;
938 looking_for_template = 0;
942 /* ID is an operator name. Duplicate the hackery in yylex to determine what
945 tree frob_opname (id)
949 frob_id (0, nth_token (0)->yychar, &id);
950 got_object = NULL_TREE;
954 /* Set up the state required to correctly handle the definition of the
955 inline function whose preparsed state has been saved in PI. */
958 begin_parsing_inclass_inline (pi)
959 struct unparsed_text *pi;
963 /* Record that we are processing the chain of inlines starting at
964 PI in a special GC root. */
965 processing_these_inlines = pi;
969 /* If this is an inline function in a local class, we must make sure
970 that we save all pertinent information about the function
971 surrounding the local class. */
972 context = decl_function_context (pi->decl);
974 push_function_context_to (context);
977 interface_unknown = pi->interface == 1;
978 interface_only = pi->interface == 0;
979 DECL_PENDING_INLINE_P (pi->decl) = 0;
980 DECL_PENDING_INLINE_INFO (pi->decl) = 0;
982 /* Pass back a handle to the rest of the inline functions, so that they
983 can be processed later. */
984 yychar = PRE_PARSED_FUNCTION_DECL;
987 start_function (NULL_TREE, pi->decl, NULL_TREE,
988 (SF_DEFAULT | SF_PRE_PARSED | SF_INCLASS_INLINE));
991 /* Called from the top level: if there are any pending inlines to
992 do, set up to process them now. This function sets up the first function
993 to be parsed; after it has been, the rule for fndef in parse.y will
994 call process_next_inline to start working on the next one. */
997 do_pending_inlines ()
999 /* Oops, we're still dealing with the last batch. */
1000 if (yychar == PRE_PARSED_FUNCTION_DECL)
1003 if (pending_inlines)
1005 /* Clear the chain, so that any inlines nested inside the batch
1006 we're to process now don't refer to this batch. See e.g.
1007 g++.other/lookup6.C. */
1008 struct unparsed_text *first = pending_inlines;
1009 pending_inlines = pending_inlines_tail = 0;
1011 begin_parsing_inclass_inline (first);
1015 /* Called from the fndecl rule in the parser when the function just parsed
1016 was declared using a PRE_PARSED_FUNCTION_DECL (i.e. came from
1017 do_pending_inlines). */
1020 process_next_inline (i)
1021 struct unparsed_text *i;
1023 tree decl = i->decl;
1024 tree context = decl_function_context (decl);
1027 pop_function_context_from (context);
1028 if (yychar == YYEMPTY)
1030 if (yychar != END_OF_SAVED_INPUT)
1031 error ("parse error at end of saved function text");
1036 begin_parsing_inclass_inline (i);
1039 processing_these_inlines = 0;
1040 extract_interface_info ();
1045 /* Subroutine of snarf_method, deals with actual absorption of the block. */
1047 static SPEW_INLINE void
1048 snarf_block (starting_file, starting_line)
1049 const char *starting_file;
1053 int look_for_semicolon = 0;
1054 int look_for_lbrac = 0;
1055 int look_for_catch = 0;
1061 /* We incremented indent_level in yylex; undo that. */
1063 else if (yychar == '=')
1064 look_for_semicolon = 1;
1065 else if (yychar == ':' || yychar == RETURN_KEYWORD || yychar == TRY)
1073 yyerror ("parse error in method specification");
1075 /* The current token is the first one to be recorded. */
1076 tmp.yychar = yychar;
1077 tmp.yylval = yylval;
1078 tmp.lineno = lineno;
1079 obstack_grow (&inline_text_obstack, &tmp, sizeof (struct token));
1083 point = obstack_object_size (&inline_text_obstack);
1084 obstack_blank (&inline_text_obstack, sizeof (struct token));
1085 yyc = add_token ((struct token *)
1086 (obstack_base (&inline_text_obstack) + point));
1093 else if (yyc == '}')
1096 if (blev == 0 && !look_for_semicolon)
1098 if (!look_for_catch)
1101 if (add_token (&tmp) != CATCH)
1108 obstack_grow (&inline_text_obstack, &tmp, sizeof (struct token));
1111 else if (yyc == ';')
1115 error ("function body for constructor missing");
1116 /* fake a { } to avoid further errors */
1117 tmp.yylval.ttype = 0;
1119 obstack_grow (&inline_text_obstack, &tmp, sizeof (struct token));
1121 obstack_grow (&inline_text_obstack, &tmp, sizeof (struct token));
1124 else if (look_for_semicolon && blev == 0)
1129 error_with_file_and_line (starting_file, starting_line,
1130 "end of file read inside definition");
1136 /* This function stores away the text for an inline function that should
1137 be processed later (by do_pending_inlines). */
1142 int starting_lineno = lineno;
1143 const char *starting_filename = input_filename;
1146 struct unparsed_text *meth;
1148 /* Leave room for the header, then absorb the block. */
1149 obstack_blank (&inline_text_obstack, sizeof (struct unparsed_text));
1150 snarf_block (starting_filename, starting_lineno);
1152 len = obstack_object_size (&inline_text_obstack);
1153 meth = (struct unparsed_text *) obstack_finish (&inline_text_obstack);
1155 /* Happens when we get two declarations of the same function in the
1157 if (decl == void_type_node
1158 || (current_class_type && TYPE_REDEFINED (current_class_type)))
1160 obstack_free (&inline_text_obstack, (char *)meth);
1165 meth->filename = starting_filename;
1166 meth->lineno = starting_lineno;
1167 meth->limit = (struct token *) ((char *)meth + len);
1168 meth->interface = (interface_unknown ? 1 : (interface_only ? 0 : 2));
1173 fprintf (stderr, "\tsaved method of %d tokens from %s:%d\n",
1174 meth->limit - (struct token *) (meth + 1),
1175 starting_filename, starting_lineno);
1178 DECL_PENDING_INLINE_INFO (decl) = meth;
1179 DECL_PENDING_INLINE_P (decl) = 1;
1181 if (pending_inlines_tail)
1182 pending_inlines_tail->next = meth;
1184 pending_inlines = meth;
1185 pending_inlines_tail = meth;
1188 /* Consume a no-commas expression - a default argument - and save it
1189 on the inline_text_obstack. */
1194 int starting_lineno = lineno;
1195 const char *starting_filename = input_filename;
1200 struct unparsed_text *buf;
1203 obstack_blank (&inline_text_obstack, sizeof (struct unparsed_text));
1207 point = obstack_object_size (&inline_text_obstack);
1208 obstack_blank (&inline_text_obstack, sizeof (struct token));
1209 yyc = add_token ((struct token *)
1210 (obstack_base (&inline_text_obstack) + point));
1212 if (plev <= 0 && (yyc == ')' || yyc == ','))
1214 else if (yyc == '(' || yyc == '[')
1216 else if (yyc == ']' || yyc == ')')
1220 error_with_file_and_line (starting_filename, starting_lineno,
1221 "end of file read inside default argument");
1226 /* Unget the last token. */
1227 push_token ((struct token *) (obstack_base (&inline_text_obstack) + point));
1228 /* This is the documented way to shrink a growing obstack block. */
1229 obstack_blank (&inline_text_obstack, - (int) sizeof (struct token));
1232 len = obstack_object_size (&inline_text_obstack);
1233 buf = (struct unparsed_text *) obstack_finish (&inline_text_obstack);
1236 buf->filename = starting_filename;
1237 buf->lineno = starting_lineno;
1238 buf->limit = (struct token *) ((char *)buf + len);
1243 fprintf (stderr, "\tsaved defarg of %d tokens from %s:%d\n",
1244 buf->limit - (struct token *) (buf + 1),
1245 starting_filename, starting_lineno);
1248 arg = make_node (DEFAULT_ARG);
1249 DEFARG_POINTER (arg) = (char *)buf;
1254 /* Decide whether the default argument we are about to see should be
1255 gobbled up as text for later parsing. */
1258 maybe_snarf_defarg ()
1260 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
1261 do_snarf_defarg = 1;
1264 /* Called from grokfndecl to note a function decl with unparsed default
1265 arguments for later processing. Also called from grokdeclarator
1266 for function types with unparsed defargs; the call from grokfndecl
1267 will always come second, so we can overwrite the entry from the type. */
1270 add_defarg_fn (decl)
1273 if (TREE_CODE (decl) == FUNCTION_DECL)
1274 TREE_VALUE (defarg_fns) = decl;
1277 defarg_fns = tree_cons (NULL_TREE, decl, defarg_fns);
1278 TREE_TYPE (defarg_fns) = current_class_type;
1282 /* Helper for do_pending_defargs. Starts the parsing of a default arg. */
1288 tree d = TREE_PURPOSE (p);
1290 feed_input ((struct unparsed_text *)DEFARG_POINTER (d));
1291 yychar = DEFARG_MARKER;
1295 /* Helper for do_pending_defargs. Ends the parsing of a default arg. */
1300 if (yychar == YYEMPTY)
1302 if (yychar != END_OF_SAVED_INPUT)
1303 error ("parse error at end of saved function text");
1308 /* Main function for deferred parsing of default arguments. Called from
1312 do_pending_defargs ()
1319 tree current = defarg_fns;
1321 tree defarg_fn = TREE_VALUE (defarg_fns);
1322 if (defarg_parm == NULL_TREE)
1324 push_nested_class (TREE_TYPE (defarg_fns), 1);
1326 if (TREE_CODE (defarg_fn) == FUNCTION_DECL)
1327 maybe_begin_member_template_processing (defarg_fn);
1329 if (TREE_CODE (defarg_fn) == FUNCTION_DECL)
1330 defarg_parm = TYPE_ARG_TYPES (TREE_TYPE (defarg_fn));
1332 defarg_parm = TYPE_ARG_TYPES (defarg_fn);
1335 defarg_parm = TREE_CHAIN (defarg_parm);
1337 for (; defarg_parm; defarg_parm = TREE_CHAIN (defarg_parm))
1338 if (!TREE_PURPOSE (defarg_parm)
1339 || TREE_CODE (TREE_PURPOSE (defarg_parm)) != DEFAULT_ARG)
1341 else if (TREE_PURPOSE (current) == error_mark_node)
1342 DEFARG_POINTER (TREE_PURPOSE (defarg_parm)) = NULL;
1345 feed_defarg (defarg_parm);
1347 /* Return to the parser, which will process this defarg
1348 and call us again. */
1352 if (TREE_CODE (defarg_fn) == FUNCTION_DECL)
1354 maybe_end_member_template_processing ();
1355 check_default_args (defarg_fn);
1359 pop_nested_class ();
1361 defarg_fns = TREE_CHAIN (defarg_fns);
1364 /* This function's default args depend on unprocessed default args
1365 of defarg_fns. We will need to reprocess this function, and
1366 check for circular dependancies. */
1369 for (a = defarg_depfns, b = TREE_PURPOSE (current); a && b;
1370 a = TREE_CHAIN (a), b = TREE_CHAIN (b))
1371 if (TREE_VALUE (a) != TREE_VALUE (b))
1376 TREE_CHAIN (current) = NULL_TREE;
1377 defarg_fns = chainon (defarg_fns, current);
1378 TREE_PURPOSE (current) = defarg_depfns;
1382 cp_warning_at ("circular dependency in default args of `%#D'", defarg_fn);
1383 /* No need to say what else is dependent, as they will be
1384 picked up in another pass. */
1386 /* Immediately repeat, but marked so that we break the loop. */
1387 defarg_fns = current;
1388 TREE_PURPOSE (current) = error_mark_node;
1390 defarg_depfns = NULL_TREE;
1392 else if (TREE_PURPOSE (current) == error_mark_node)
1393 defarg_fnsdone = tree_cons (NULL_TREE, defarg_fn, defarg_fnsdone);
1397 /* After parsing all the default arguments, we must clear any that remain,
1398 which will be part of a circular dependency. */
1400 done_pending_defargs ()
1402 for (; defarg_fnsdone; defarg_fnsdone = TREE_CHAIN (defarg_fnsdone))
1404 tree fn = TREE_VALUE (defarg_fnsdone);
1407 if (TREE_CODE (fn) == FUNCTION_DECL)
1408 parms = TYPE_ARG_TYPES (TREE_TYPE (fn));
1410 parms = TYPE_ARG_TYPES (fn);
1411 for (; parms; parms = TREE_CHAIN (parms))
1412 if (TREE_PURPOSE (parms)
1413 && TREE_CODE (TREE_PURPOSE (parms)) == DEFAULT_ARG)
1415 my_friendly_assert (!DEFARG_POINTER (TREE_PURPOSE (parms)), 20010107);
1416 TREE_PURPOSE (parms) = NULL_TREE;
1421 /* In processing the current default arg, we called FN, but that call
1422 required a default argument of FN, and that had not yet been processed.
1426 unprocessed_defarg_fn (fn)
1429 defarg_depfns = tree_cons (NULL_TREE, fn, defarg_depfns);
1432 /* Called from the parser to update an element of TYPE_ARG_TYPES for some
1433 FUNCTION_TYPE with the newly parsed version of its default argument, which
1434 was previously digested as text. */
1437 replace_defarg (arg, init)
1440 if (init == error_mark_node)
1441 TREE_PURPOSE (arg) = error_mark_node;
1444 if (! processing_template_decl
1445 && ! can_convert_arg (TREE_VALUE (arg), TREE_TYPE (init), init))
1446 cp_pedwarn ("invalid type `%T' for default argument to `%T'",
1447 TREE_TYPE (init), TREE_VALUE (arg));
1449 TREE_PURPOSE (arg) = init;
1454 /* debug_yychar takes a yychar (token number) value and prints its name. */
1461 fprintf (stderr, "->%d < %c >\n", lineno, yy);
1462 else if (yy == IDENTIFIER || yy == TYPENAME)
1465 if (TREE_CODE (yylval.ttype) == IDENTIFIER_NODE)
1466 id = IDENTIFIER_POINTER (yylval.ttype);
1467 else if (TREE_CODE_CLASS (TREE_CODE (yylval.ttype)) == 'd')
1468 id = IDENTIFIER_POINTER (DECL_NAME (yylval.ttype));
1471 fprintf (stderr, "->%d <%s `%s'>\n", lineno, debug_yytranslate (yy), id);
1474 fprintf (stderr, "->%d <%s>\n", lineno, debug_yytranslate (yy));
1479 #define NAME(type) cpp_type2name (type)
1485 const char *string = _(msgid);
1487 if (last_token == CPP_EOF)
1488 error ("%s at end of input", string);
1489 else if (last_token == CPP_CHAR || last_token == CPP_WCHAR)
1491 unsigned int val = TREE_INT_CST_LOW (yylval.ttype);
1492 const char *ell = (last_token == CPP_CHAR) ? "" : "L";
1493 if (val <= UCHAR_MAX && ISGRAPH (val))
1494 error ("%s before %s'%c'", string, ell, val);
1496 error ("%s before %s'\\x%x'", string, ell, val);
1498 else if (last_token == CPP_STRING
1499 || last_token == CPP_WSTRING)
1500 error ("%s before string constant", string);
1501 else if (last_token == CPP_NUMBER
1502 || last_token == CPP_INT
1503 || last_token == CPP_FLOAT)
1504 error ("%s before numeric constant", string);
1505 else if (last_token == CPP_NAME)
1507 if (yylval.ttype && TREE_CODE (yylval.ttype) == IDENTIFIER_NODE)
1508 error ("%s before `%s'", string, IDENTIFIER_POINTER (yylval.ttype));
1510 error ("%s before `%c'", string, yychar);
1513 error ("%s before `%s' token", string, NAME (last_token));