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)
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. */
58 YYSTYPE GTY ((desc ("%1.yychar"))) 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
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 #define TOKEN_CHUNK_SIZE 20
72 struct token_chunk GTY(())
74 struct token_chunk *next;
75 struct token toks[TOKEN_CHUNK_SIZE];
78 struct unparsed_text GTY(())
80 struct unparsed_text *next; /* process this one next */
81 tree decl; /* associated declaration */
82 location_t locus; /* location we got the text from */
83 int interface; /* remembering interface_unknown and interface_only */
85 struct token_chunk * tokens; /* Start of the token list. */
87 struct token_chunk *last_chunk; /* End of the token list. */
88 short last_pos; /* Number of tokens used in the last chunk of
91 short cur_pos; /* Current token in 'cur_chunk', when rescanning. */
92 struct token_chunk *cur_chunk; /* Current chunk, when rescanning. */
95 /* Stack of state saved off when we return to an inline method or
96 default argument that has been stored for later parsing. */
99 struct unparsed_text *input;
102 YYSTYPE GTY ((desc ("%1.yychar"))) yylval;
104 struct obstack GTY ((skip (""))) token_obstack;
108 static GTY(()) struct feed *feed;
110 static SPEW_INLINE void do_aggr PARAMS ((void));
111 static SPEW_INLINE int identifier_type PARAMS ((tree));
112 static void scan_tokens PARAMS ((int));
113 static void feed_defarg PARAMS ((tree));
114 static void finish_defarg PARAMS ((void));
115 static void yylexstring PARAMS ((struct token *));
116 static int read_token PARAMS ((struct token *));
118 static SPEW_INLINE int num_tokens PARAMS ((void));
119 static SPEW_INLINE struct token *nth_token PARAMS ((int));
120 static SPEW_INLINE int next_token PARAMS ((struct token *));
121 static SPEW_INLINE int shift_token PARAMS ((void));
122 static SPEW_INLINE void push_token PARAMS ((struct token *));
123 static SPEW_INLINE void consume_token PARAMS ((void));
124 static SPEW_INLINE int read_process_identifier PARAMS ((YYSTYPE *));
126 static SPEW_INLINE void feed_input PARAMS ((struct unparsed_text *));
127 static SPEW_INLINE struct token * space_for_token
128 PARAMS ((struct unparsed_text *t));
129 static SPEW_INLINE struct token * remove_last_token
130 PARAMS ((struct unparsed_text *t));
131 static struct unparsed_text * alloc_unparsed_text
132 PARAMS ((const location_t *, tree decl, int interface));
134 static void snarf_block PARAMS ((struct unparsed_text *t));
135 static tree snarf_defarg PARAMS ((void));
136 static int frob_id PARAMS ((int, int, tree *));
138 /* The list of inline functions being held off until we reach the end of
139 the current class declaration. */
140 static GTY(()) struct unparsed_text *pending_inlines;
141 static GTY(()) struct unparsed_text *pending_inlines_tail;
143 /* The list of previously-deferred inline functions currently being parsed.
144 This exists solely to be a GC root. */
145 static GTY(()) struct unparsed_text *processing_these_inlines;
147 static void begin_parsing_inclass_inline PARAMS ((struct unparsed_text *));
151 static unsigned int yylex_ctr = 0;
153 static void debug_yychar PARAMS ((int));
156 extern char *debug_yytranslate PARAMS ((int));
158 static enum cpp_ttype last_token;
159 static tree last_token_id;
162 /* the declaration found for the last IDENTIFIER token read in. yylex
163 must look this up to detect typedefs, which get token type
164 tTYPENAME, so it is left around in case the identifier is not a
165 typedef but is used in a context which makes it a reference to a
167 extern tree lastiddecl; /* let our brains leak out here too */
168 extern int yychar; /* the lookahead symbol */
169 extern YYSTYPE yylval; /* the semantic value of the */
170 /* lookahead symbol */
171 /* The token fifo lives in this obstack. */
172 static struct obstack token_obstack;
173 static int first_token;
175 /* When we see a default argument in a method declaration, we snarf it as
176 text using snarf_defarg. When we get up to namespace scope, we then go
177 through and parse all of them using do_pending_defargs. Since yacc
178 parsers are not reentrant, we retain defargs state in these two
179 variables so that subsequent calls to do_pending_defargs can resume
180 where the previous call left off. DEFARG_FNS is a tree_list where
181 the TREE_TYPE is the current_class_type, TREE_VALUE is the FUNCTION_DECL,
182 and TREE_PURPOSE is the list unprocessed dependent functions. */
184 /* list of functions with unprocessed defargs */
185 static GTY(()) tree defarg_fns;
186 /* current default parameter */
187 static GTY(()) tree defarg_parm;
188 /* list of unprocessed fns met during current fn. */
189 static GTY(()) tree defarg_depfns;
190 /* list of fns with circular defargs */
191 static GTY(()) tree defarg_fnsdone;
193 /* Initialize obstacks. Called once, from cxx_init. */
198 gcc_obstack_init (&token_obstack);
201 /* Subroutine of read_token. */
202 static SPEW_INLINE int
203 read_process_identifier (pyylval)
206 tree id = pyylval->ttype;
208 if (C_IS_RESERVED_WORD (id))
210 pyylval->ttype = ridpointers[C_RID_CODE (id)];
211 return C_RID_YYCODE (id);
214 /* Make sure that user does not collide with our internal naming
215 scheme. This is not necessary if '.' is used to remove them from
216 the user's namespace, but is if '$' or double underscores are. */
218 #if !defined(JOINER) || JOINER == '$'
220 || VTABLE_NAME_P (id)
222 || ANON_AGGRNAME_P (id))
224 "identifier name `%s' conflicts with GNU C++ internal naming strategy",
225 IDENTIFIER_POINTER (id));
230 /* Concatenate strings before returning them to the parser. This isn't quite
231 as good as having it done in the lexer, but it's better than nothing. */
237 enum cpp_ttype next_type;
240 next_type = c_lex (&next);
241 if (next_type == CPP_STRING || next_type == CPP_WSTRING)
245 VARRAY_TREE_INIT (strings, 32, "strings");
246 VARRAY_PUSH_TREE (strings, t->yylval.ttype);
250 VARRAY_PUSH_TREE (strings, next);
251 next_type = c_lex (&next);
253 while (next_type == CPP_STRING || next_type == CPP_WSTRING);
255 t->yylval.ttype = combine_strings (strings);
256 last_token_id = t->yylval.ttype;
259 /* We will have always read one token too many. */
260 _cpp_backup_tokens (parse_in, 1);
265 /* Read the next token from the input file. The token is written into
266 T, and its type number is returned. */
273 last_token = c_lex (&last_token_id);
274 t->yylval.ttype = last_token_id;
278 #define YYCHAR(YY) t->yychar = (YY); break;
279 #define YYCODE(C) t->yylval.code = (C);
281 case CPP_EQ: YYCHAR('=');
282 case CPP_NOT: YYCHAR('!');
283 case CPP_GREATER: YYCODE(GT_EXPR); YYCHAR('>');
284 case CPP_LESS: YYCODE(LT_EXPR); YYCHAR('<');
285 case CPP_PLUS: YYCODE(PLUS_EXPR); YYCHAR('+');
286 case CPP_MINUS: YYCODE(MINUS_EXPR); YYCHAR('-');
287 case CPP_MULT: YYCODE(MULT_EXPR); YYCHAR('*');
288 case CPP_DIV: YYCODE(TRUNC_DIV_EXPR); YYCHAR('/');
289 case CPP_MOD: YYCODE(TRUNC_MOD_EXPR); YYCHAR('%');
290 case CPP_AND: YYCODE(BIT_AND_EXPR); YYCHAR('&');
291 case CPP_OR: YYCODE(BIT_IOR_EXPR); YYCHAR('|');
292 case CPP_XOR: YYCODE(BIT_XOR_EXPR); YYCHAR('^');
293 case CPP_RSHIFT: YYCODE(RSHIFT_EXPR); YYCHAR(RSHIFT);
294 case CPP_LSHIFT: YYCODE(LSHIFT_EXPR); YYCHAR(LSHIFT);
296 case CPP_COMPL: YYCHAR('~');
297 case CPP_AND_AND: YYCHAR(ANDAND);
298 case CPP_OR_OR: YYCHAR(OROR);
299 case CPP_QUERY: YYCHAR('?');
300 case CPP_COLON: YYCHAR(':');
301 case CPP_COMMA: YYCHAR(',');
302 case CPP_OPEN_PAREN: YYCHAR('(');
303 case CPP_CLOSE_PAREN: YYCHAR(')');
304 case CPP_EQ_EQ: YYCODE(EQ_EXPR); YYCHAR(EQCOMPARE);
305 case CPP_NOT_EQ: YYCODE(NE_EXPR); YYCHAR(EQCOMPARE);
306 case CPP_GREATER_EQ:YYCODE(GE_EXPR); YYCHAR(ARITHCOMPARE);
307 case CPP_LESS_EQ: YYCODE(LE_EXPR); YYCHAR(ARITHCOMPARE);
309 case CPP_PLUS_EQ: YYCODE(PLUS_EXPR); YYCHAR(ASSIGN);
310 case CPP_MINUS_EQ: YYCODE(MINUS_EXPR); YYCHAR(ASSIGN);
311 case CPP_MULT_EQ: YYCODE(MULT_EXPR); YYCHAR(ASSIGN);
312 case CPP_DIV_EQ: YYCODE(TRUNC_DIV_EXPR); YYCHAR(ASSIGN);
313 case CPP_MOD_EQ: YYCODE(TRUNC_MOD_EXPR); YYCHAR(ASSIGN);
314 case CPP_AND_EQ: YYCODE(BIT_AND_EXPR); YYCHAR(ASSIGN);
315 case CPP_OR_EQ: YYCODE(BIT_IOR_EXPR); YYCHAR(ASSIGN);
316 case CPP_XOR_EQ: YYCODE(BIT_XOR_EXPR); YYCHAR(ASSIGN);
317 case CPP_RSHIFT_EQ: YYCODE(RSHIFT_EXPR); YYCHAR(ASSIGN);
318 case CPP_LSHIFT_EQ: YYCODE(LSHIFT_EXPR); YYCHAR(ASSIGN);
320 case CPP_OPEN_SQUARE: YYCHAR('[');
321 case CPP_CLOSE_SQUARE: YYCHAR(']');
322 case CPP_OPEN_BRACE: YYCHAR('{');
323 case CPP_CLOSE_BRACE: YYCHAR('}');
324 case CPP_SEMICOLON: YYCHAR(';');
325 case CPP_ELLIPSIS: YYCHAR(ELLIPSIS);
327 case CPP_PLUS_PLUS: YYCHAR(PLUSPLUS);
328 case CPP_MINUS_MINUS: YYCHAR(MINUSMINUS);
329 case CPP_DEREF: YYCHAR(POINTSAT);
330 case CPP_DOT: YYCHAR('.');
332 /* These tokens are C++ specific. */
333 case CPP_SCOPE: YYCHAR(SCOPE);
334 case CPP_DEREF_STAR: YYCHAR(POINTSAT_STAR);
335 case CPP_DOT_STAR: YYCHAR(DOT_STAR);
336 case CPP_MIN_EQ: YYCODE(MIN_EXPR); YYCHAR(ASSIGN);
337 case CPP_MAX_EQ: YYCODE(MAX_EXPR); YYCHAR(ASSIGN);
338 case CPP_MIN: YYCODE(MIN_EXPR); YYCHAR(MIN_MAX);
339 case CPP_MAX: YYCODE(MAX_EXPR); YYCHAR(MIN_MAX);
348 t->yychar = read_process_identifier (&t->yylval);
354 t->yychar = CONSTANT;
363 yyerror ("parse error");
373 struct unparsed_text *input;
381 f = ggc_alloc (sizeof (struct feed));
383 input->cur_chunk = input->tokens;
388 fprintf (stderr, "\tfeeding %s:%d [%d tokens]\n",
389 input->locus.file, input->locus.line, input->limit - input->pos);
393 f->locus.file = input_filename;
394 f->locus.line = lineno;
397 f->first_token = first_token;
398 f->token_obstack = token_obstack;
401 input_filename = input->locus.file;
402 lineno = input->locus.line;
404 yylval.ttype = NULL_TREE;
406 gcc_obstack_init (&token_obstack);
413 struct feed *f = feed;
415 input_filename = f->locus.file;
416 lineno = f->locus.line;
419 first_token = f->first_token;
420 obstack_free (&token_obstack, 0);
421 token_obstack = f->token_obstack;
426 fprintf (stderr, "\treturning to %s:%d\n", input_filename, lineno);
430 /* Token queue management. */
432 /* Return the number of tokens available on the fifo. */
433 static SPEW_INLINE int
436 return (obstack_object_size (&token_obstack) / sizeof (struct token))
440 /* Fetch the token N down the line from the head of the fifo. */
442 static SPEW_INLINE struct token*
446 #ifdef ENABLE_CHECKING
447 /* could just have this do slurp_ implicitly, but this way is easier
449 my_friendly_assert (n >= 0 && n < num_tokens (), 298);
451 return ((struct token*)obstack_base (&token_obstack)) + n + first_token;
454 static const struct token Teosi = { END_OF_SAVED_INPUT, 0 UNION_INIT_ZERO };
455 static const struct token Tpad = { EMPTY, 0 UNION_INIT_ZERO };
457 /* Copy the next token into T and return its value. */
458 static SPEW_INLINE int
463 return read_token (t);
465 if (feed->input->cur_chunk != feed->input->last_chunk
466 || feed->input->cur_pos != feed->input->last_pos)
468 if (feed->input->cur_pos == TOKEN_CHUNK_SIZE)
470 feed->input->cur_chunk = feed->input->cur_chunk->next;
471 feed->input->cur_pos = 0;
473 memcpy (t, feed->input->cur_chunk->toks + feed->input->cur_pos,
474 sizeof (struct token));
475 feed->input->cur_pos++;
479 memcpy (t, &Teosi, sizeof (struct token));
480 return END_OF_SAVED_INPUT;
483 /* Shift the next token onto the fifo. */
484 static SPEW_INLINE int
487 size_t point = obstack_object_size (&token_obstack);
488 obstack_blank (&token_obstack, sizeof (struct token));
489 return next_token ((struct token *) (obstack_base (&token_obstack) + point));
492 /* Consume the next token out of the fifo. */
494 static SPEW_INLINE void
497 if (num_tokens () == 1)
499 obstack_free (&token_obstack, obstack_base (&token_obstack));
506 /* Push a token at the head of the queue; it will be the next token read. */
507 static SPEW_INLINE void
511 if (first_token == 0) /* We hope this doesn't happen often. */
513 size_t active = obstack_object_size (&token_obstack);
514 obstack_blank (&token_obstack, sizeof (struct token));
516 memmove (obstack_base (&token_obstack) + sizeof (struct token),
517 obstack_base (&token_obstack), active);
521 memcpy (nth_token (0), t, sizeof (struct token));
525 /* Pull in enough tokens that the queue is N long beyond the current
533 int num = num_tokens ();
536 /* First, prune any empty tokens at the end. */
538 while (i > 0 && nth_token (i - 1)->yychar == EMPTY)
542 obstack_blank (&token_obstack, -((num - i) * sizeof (struct token)));
546 /* Now, if we already have enough tokens, return. */
550 /* Never read past these characters: they might separate
551 the current input stream from one we save away later. */
552 for (i = 0; i < num; i++)
554 yychar = nth_token (i)->yychar;
555 if (yychar == '{' || yychar == ':' || yychar == ';')
559 while (num_tokens () <= n)
561 yychar = shift_token ();
562 if (yychar == '{' || yychar == ':' || yychar == ';')
568 while (num_tokens () <= n)
569 obstack_grow (&token_obstack, &Tpad, sizeof (struct token));
572 int looking_for_typename;
573 int looking_for_template;
575 static int after_friend;
576 static int after_new;
577 static int do_snarf_defarg;
582 static SPEW_INLINE int
583 identifier_type (decl)
588 if (TREE_CODE (decl) == TEMPLATE_DECL)
590 if (TREE_CODE (DECL_TEMPLATE_RESULT (decl)) == TYPE_DECL)
592 else if (looking_for_template)
595 if (looking_for_template && really_overloaded_fn (decl))
597 /* See through a baselink. */
598 if (TREE_CODE (decl) == TREE_LIST)
599 decl = TREE_VALUE (decl);
601 for (t = decl; t != NULL_TREE; t = OVL_CHAIN (t))
602 if (DECL_FUNCTION_TEMPLATE_P (OVL_FUNCTION (t)))
605 if (TREE_CODE (decl) == NAMESPACE_DECL)
607 if (TREE_CODE (decl) != TYPE_DECL)
609 if (DECL_ARTIFICIAL (decl) && TREE_TYPE (decl) == current_class_type)
612 /* A constructor declarator for a template type will get here as an
613 implicit typename, a TYPENAME_TYPE with a type. */
615 if (t && TREE_CODE (t) == TYPENAME_TYPE)
617 decl = TREE_TYPE (decl);
618 if (TREE_CODE (decl) == TYPENAME_TYPE)
619 decl = TREE_TYPE (decl);
626 /* token[0] == AGGR (struct/union/enum)
627 Thus, token[1] is either a tTYPENAME or a TYPENAME_DEFN.
628 If token[2] == '{' or ':' then it's TYPENAME_DEFN.
629 It's also a definition if it's a forward declaration (as in 'struct Foo;')
630 which we can tell if token[2] == ';' *and* token[-1] != FRIEND or NEW. */
632 static SPEW_INLINE void
638 yc1 = nth_token (1)->yychar;
639 if (yc1 != tTYPENAME && yc1 != IDENTIFIER && yc1 != PTYPENAME)
641 yc2 = nth_token (2)->yychar;
644 /* It's a forward declaration iff we were not preceded by
645 'friend' or `new'. */
646 if (after_friend || after_new)
649 else if (yc2 != '{' && yc2 != ':')
655 nth_token (1)->yychar = TYPENAME_DEFN;
658 nth_token (1)->yychar = PTYPENAME_DEFN;
661 nth_token (1)->yychar = IDENTIFIER_DEFN;
671 /* Only types expected, not even namespaces. */
672 looking_for_typename = 2;
674 if ((yychar = yylex ()) < 0) yychar = 0;
675 looking_for_typename = 0;
676 if (yychar == IDENTIFIER)
678 lastiddecl = lookup_name (yylval.ttype, -2);
680 yychar = identifier_type (lastiddecl);
688 int old_looking_for_typename = 0;
689 int just_saw_new = 0;
690 int just_saw_friend = 0;
692 timevar_push (TV_LEX);
699 fprintf (stderr, "\t\t## %d @%d ", yylex_ctr, lineno);
706 yylval.ttype = snarf_defarg ();
708 got_object = NULL_TREE;
709 timevar_pop (TV_LEX);
713 /* if we've got tokens, send them */
714 else if (num_tokens ())
715 yychr = nth_token (0)->yychar;
717 yychr = shift_token ();
719 /* many tokens just need to be returned. At first glance, all we
720 have to do is send them back up, but some of them are needed to
721 figure out local context. */
725 /* This is a lexical no-op. */
728 debug_yychar (yychr);
735 if (nth_token (1)->yychar == ')')
747 peek = nth_token (1)->yychar;
748 yychr = frob_id (yychr, peek, &nth_token (0)->yylval.ttype);
751 case IDENTIFIER_DEFN:
756 /* If we see a SCOPE next, restore the old value.
757 Otherwise, we got what we want. */
758 looking_for_typename = old_looking_for_typename;
759 looking_for_template = 0;
763 if (nth_token (0)->yylval.ttype == ridpointers[RID_EXTERN])
766 if (nth_token (1)->yychar == STRING)
768 yychr = EXTERN_LANG_STRING;
769 nth_token (1)->yylval.ttype = get_identifier
770 (TREE_STRING_POINTER (nth_token (1)->yylval.ttype));
774 /* do_aggr needs to know if the previous token was `friend'. */
775 else if (nth_token (0)->yylval.ttype == ridpointers[RID_FRIEND])
781 /* do_aggr needs to know if the previous token was `new'. */
789 /* If this provides a type for us, then revert lexical
790 state to standard state. */
791 looking_for_typename = 0;
799 /* Set this again, in case we are rescanning. */
800 looking_for_typename = 2;
807 after_friend = just_saw_friend;
808 after_new = just_saw_new;
810 /* class member lookup only applies to the first token after the object
811 expression, except for explicit destructor calls. */
813 got_object = NULL_TREE;
817 struct token *tok = nth_token (0);
819 yylval = tok->yylval;
821 lineno = tok->lineno;
826 debug_yychar (yychr);
830 timevar_pop (TV_LEX);
834 /* Unget character CH from the input stream.
835 If RESCAN is non-zero, then we want to `see' this
836 character as the next input token. */
839 yyungetc (ch, rescan)
843 /* Unget a character from the input stream. */
844 if (yychar == YYEMPTY || rescan == 0)
849 fake.yylval.ttype = 0;
850 fake.lineno = lineno;
860 /* Lexer hackery to determine what *IDP really is. */
863 frob_id (yyc, peek, idp)
869 int old_looking_for_typename = 0;
873 /* Don't interfere with the setting from an 'aggr' prefix. */
874 old_looking_for_typename = looking_for_typename;
875 looking_for_typename = 1;
877 else if (peek == '<')
878 looking_for_template = 1;
879 trrr = lookup_name (*idp, -2);
882 yyc = identifier_type (trrr);
889 /* If this got special lookup, remember it. In these
890 cases, we know it can't be a declarator-id. */
891 if (got_scope || got_object)
903 lastiddecl = NULL_TREE;
904 got_scope = NULL_TREE;
905 looking_for_typename = old_looking_for_typename;
906 looking_for_template = 0;
910 /* ID is an operator name. Duplicate the hackery in yylex to determine what
913 tree frob_opname (id)
917 frob_id (0, nth_token (0)->yychar, &id);
918 got_object = NULL_TREE;
922 /* Set up the state required to correctly handle the definition of the
923 inline function whose preparsed state has been saved in PI. */
926 begin_parsing_inclass_inline (pi)
927 struct unparsed_text *pi;
931 /* Record that we are processing the chain of inlines starting at
934 cp_function_chain->unparsed_inlines = pi;
936 processing_these_inlines = pi;
940 /* If this is an inline function in a local class, we must make sure
941 that we save all pertinent information about the function
942 surrounding the local class. */
943 context = decl_function_context (pi->decl);
945 push_function_context_to (context);
948 interface_unknown = pi->interface == 1;
949 interface_only = pi->interface == 0;
950 DECL_PENDING_INLINE_P (pi->decl) = 0;
951 DECL_PENDING_INLINE_INFO (pi->decl) = 0;
953 /* Pass back a handle to the rest of the inline functions, so that they
954 can be processed later. */
955 yychar = PRE_PARSED_FUNCTION_DECL;
958 start_function (NULL_TREE, pi->decl, NULL_TREE,
959 (SF_DEFAULT | SF_PRE_PARSED | SF_INCLASS_INLINE));
962 /* Called from the top level: if there are any pending inlines to
963 do, set up to process them now. This function sets up the first function
964 to be parsed; after it has been, the rule for fndef in parse.y will
965 call process_next_inline to start working on the next one. */
968 do_pending_inlines ()
970 /* Oops, we're still dealing with the last batch. */
971 if (yychar == PRE_PARSED_FUNCTION_DECL)
976 /* Clear the chain, so that any inlines nested inside the batch
977 we're to process now don't refer to this batch. See e.g.
978 g++.other/lookup6.C. */
979 struct unparsed_text *first = pending_inlines;
980 pending_inlines = pending_inlines_tail = 0;
982 begin_parsing_inclass_inline (first);
986 /* Called from the fndecl rule in the parser when the function just parsed
987 was declared using a PRE_PARSED_FUNCTION_DECL (i.e. came from
988 do_pending_inlines). */
991 process_next_inline (i)
992 struct unparsed_text *i;
995 tree context = decl_function_context (decl);
998 pop_function_context_from (context);
999 if (yychar == YYEMPTY)
1001 if (yychar != END_OF_SAVED_INPUT)
1002 error ("parse error at end of saved function text");
1007 begin_parsing_inclass_inline (i);
1011 cp_function_chain->unparsed_inlines = 0;
1013 processing_these_inlines = 0;
1014 extract_interface_info ();
1018 /* Create a new token at the end of the token list in T. */
1019 static SPEW_INLINE struct token *
1021 struct unparsed_text *t;
1023 if (t->last_pos != TOKEN_CHUNK_SIZE)
1024 return t->last_chunk->toks + (t->last_pos++);
1026 t->last_chunk->next = ggc_alloc (sizeof (*t->last_chunk->next));
1027 t->last_chunk = t->last_chunk->next;
1028 t->last_chunk->next = NULL;
1031 return t->last_chunk->toks;
1034 /* Shrink the token list in T by one token. */
1035 static SPEW_INLINE struct token *
1036 remove_last_token (t)
1037 struct unparsed_text *t;
1039 struct token *result = t->last_chunk->toks + t->last_pos - 1;
1040 if (t->last_pos == 0)
1043 if (t->last_pos == 0 && t->last_chunk != t->tokens)
1045 struct token_chunk **tc;
1046 for (tc = &t->tokens; (*tc)->next != NULL; tc = &(*tc)->next)
1049 t->last_pos = ARRAY_SIZE ((*tc)->toks);
1054 /* Allocate an 'unparsed_text' structure, ready to use space_for_token. */
1055 static struct unparsed_text *
1056 alloc_unparsed_text (locus, decl, interface)
1057 const location_t *locus;
1061 struct unparsed_text *r;
1062 r = ggc_alloc_cleared (sizeof (*r));
1065 r->interface = interface;
1066 r->tokens = r->last_chunk = ggc_alloc_cleared (sizeof (*r->tokens));
1070 /* Subroutine of snarf_method, deals with actual absorption of the block. */
1074 struct unparsed_text *t;
1077 int look_for_semicolon = 0;
1078 int look_for_lbrac = 0;
1079 int look_for_catch = 0;
1081 struct token *current;
1085 else if (yychar == '=')
1086 look_for_semicolon = 1;
1087 else if (yychar == ':' || yychar == RETURN_KEYWORD || yychar == TRY)
1095 yyerror ("parse error in method specification");
1097 /* The current token is the first one to be recorded. */
1098 current = space_for_token (t);
1099 current->yychar = yychar;
1100 current->yylval = yylval;
1101 current->lineno = lineno;
1105 yyc = next_token (space_for_token (t));
1112 else if (yyc == '}')
1115 if (blev == 0 && !look_for_semicolon)
1117 if (!look_for_catch)
1120 if (next_token (space_for_token (t)) != CATCH)
1122 push_token (remove_last_token (t));
1129 else if (yyc == ';')
1135 error ("function body for constructor missing");
1136 /* fake a { } to avoid further errors */
1137 fake = space_for_token (t);
1138 fake->yylval.ttype = 0;
1140 fake = space_for_token (t);
1141 fake->yylval.ttype = 0;
1145 else if (look_for_semicolon && blev == 0)
1150 error ("%Hend of file read inside definition", &t->locus);
1156 /* This function stores away the text for an inline function that should
1157 be processed later (by do_pending_inlines). */
1162 struct unparsed_text *meth;
1163 location_t starting;
1164 starting.file = input_filename;
1165 starting.line = lineno;
1167 meth = alloc_unparsed_text (&starting, decl, (interface_unknown ? 1
1168 : (interface_only ? 0 : 2)));
1172 /* Happens when we get two declarations of the same function in the
1174 if (decl == void_type_node
1175 || (current_class_type && TYPE_REDEFINED (current_class_type)))
1180 fprintf (stderr, "\tsaved method of %d tokens from %s:%d\n",
1181 meth->limit, starting.file, starting.line);
1184 DECL_PENDING_INLINE_INFO (decl) = meth;
1185 DECL_PENDING_INLINE_P (decl) = 1;
1187 if (pending_inlines_tail)
1188 pending_inlines_tail->next = meth;
1190 pending_inlines = meth;
1191 pending_inlines_tail = meth;
1194 /* Consume a no-commas expression - a default argument - and return
1195 a DEFAULT_ARG tree node. */
1202 struct unparsed_text *buf;
1204 location_t starting;
1205 starting.file = input_filename;
1206 starting.line = lineno;
1208 buf = alloc_unparsed_text (&starting, 0, 0);
1212 yyc = next_token (space_for_token (buf));
1214 if (plev <= 0 && (yyc == ')' || yyc == ','))
1216 else if (yyc == '(' || yyc == '[')
1218 else if (yyc == ']' || yyc == ')')
1222 error ("%Hend of file read inside default argument", &starting);
1227 /* Unget the last token. */
1228 push_token (remove_last_token (buf));
1233 fprintf (stderr, "\tsaved defarg of %d tokens from %s:%d\n",
1234 buf->limit, starting.file, starting.line);
1237 arg = make_node (DEFAULT_ARG);
1238 DEFARG_POINTER (arg) = (char *)buf;
1243 /* Decide whether the default argument we are about to see should be
1244 gobbled up as text for later parsing. */
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. */
1259 add_defarg_fn (decl)
1262 if (TREE_CODE (decl) == FUNCTION_DECL)
1263 TREE_VALUE (defarg_fns) = decl;
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. */
1277 tree d = TREE_PURPOSE (p);
1279 feed_input ((struct unparsed_text *)DEFARG_POINTER (d));
1280 yychar = DEFARG_MARKER;
1284 /* Helper for do_pending_defargs. Ends the parsing of a default arg. */
1289 if (yychar == YYEMPTY)
1291 if (yychar != END_OF_SAVED_INPUT)
1292 error ("parse error at end of saved function text");
1297 /* Main function for deferred parsing of default arguments. Called from
1301 do_pending_defargs ()
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);
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));
1321 defarg_parm = TYPE_ARG_TYPES (defarg_fn);
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)
1330 else if (TREE_PURPOSE (current) == error_mark_node)
1331 DEFARG_POINTER (TREE_PURPOSE (defarg_parm)) = NULL;
1334 feed_defarg (defarg_parm);
1336 /* Return to the parser, which will process this defarg
1337 and call us again. */
1341 if (TREE_CODE (defarg_fn) == FUNCTION_DECL)
1343 maybe_end_member_template_processing ();
1344 check_default_args (defarg_fn);
1348 pop_nested_class ();
1350 defarg_fns = TREE_CHAIN (defarg_fns);
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. */
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))
1365 TREE_CHAIN (current) = NULL_TREE;
1366 defarg_fns = chainon (defarg_fns, current);
1367 TREE_PURPOSE (current) = defarg_depfns;
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. */
1389 done_pending_defargs ()
1391 for (; defarg_fnsdone; defarg_fnsdone = TREE_CHAIN (defarg_fnsdone))
1393 tree fn = TREE_VALUE (defarg_fnsdone);
1396 if (TREE_CODE (fn) == FUNCTION_DECL)
1397 parms = TYPE_ARG_TYPES (TREE_TYPE (fn));
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.
1415 unprocessed_defarg_fn (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. */
1426 replace_defarg (arg, init)
1429 if (init == error_mark_node)
1430 TREE_PURPOSE (arg) = error_mark_node;
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));
1438 TREE_PURPOSE (arg) = init;
1443 /* debug_yychar takes a yychar (token number) value and prints its name. */
1450 fprintf (stderr, "->%d < %c >\n", lineno, yy);
1451 else if (yy == IDENTIFIER || yy == tTYPENAME)
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));
1460 fprintf (stderr, "->%d <%s `%s'>\n", lineno, debug_yytranslate (yy), id);
1463 fprintf (stderr, "->%d <%s>\n", lineno, debug_yytranslate (yy));
1468 #define NAME(TYPE) cpp_type2name (TYPE)
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);
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);
1499 error ("%s before `\%o'", string, yychar);
1502 error ("%s before `%s' token", string, NAME (last_token));
1505 #include "gt-cp-spew.h"