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. */
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 void yylexstring
PARAMS ((struct token
*));
106 static int read_token
PARAMS ((struct token
*));
108 static SPEW_INLINE
int num_tokens
PARAMS ((void));
109 static SPEW_INLINE
struct token
*nth_token
PARAMS ((int));
110 static SPEW_INLINE
int add_token
PARAMS ((struct token
*));
111 static SPEW_INLINE
int shift_token
PARAMS ((void));
112 static SPEW_INLINE
void push_token
PARAMS ((struct token
*));
113 static SPEW_INLINE
void consume_token
PARAMS ((void));
114 static SPEW_INLINE
int read_process_identifier
PARAMS ((YYSTYPE
*));
116 static SPEW_INLINE
void feed_input
PARAMS ((struct unparsed_text
*));
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 static struct unparsed_text
*pending_inlines
;
124 static 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 static struct unparsed_text
*processing_these_inlines
;
130 static void begin_parsing_inclass_inline
PARAMS ((struct unparsed_text
*));
134 static unsigned int yylex_ctr
= 0;
136 static void debug_yychar
PARAMS ((int));
139 extern char *debug_yytranslate
PARAMS ((int));
141 static enum cpp_ttype last_token
;
142 static tree last_token_id
;
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 cxx_init. */
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 pyylval
->ttype
= ridpointers
[C_RID_CODE (id
)];
226 return C_RID_YYCODE (id
);
230 /* Make sure that user does not collide with our internal naming
231 scheme. This is not necessary if '.' is used to remove them from
232 the user's namespace, but is if '$' or double underscores are. */
234 #if !defined(JOINER) || JOINER == '$'
236 || VTABLE_NAME_P (id
)
238 || ANON_AGGRNAME_P (id
))
240 "identifier name `%s' conflicts with GNU C++ internal naming strategy",
241 IDENTIFIER_POINTER (id
));
246 /* Concatenate strings before returning them to the parser. This isn't quite
247 as good as having it done in the lexer, but it's better than nothing. */
253 enum cpp_ttype next_type
;
256 next_type
= c_lex (&next
);
257 if (next_type
== CPP_STRING
|| next_type
== CPP_WSTRING
)
261 VARRAY_TREE_INIT (strings
, 32, "strings");
262 VARRAY_PUSH_TREE (strings
, t
->yylval
.ttype
);
266 VARRAY_PUSH_TREE (strings
, next
);
267 next_type
= c_lex (&next
);
269 while (next_type
== CPP_STRING
|| next_type
== CPP_WSTRING
);
271 t
->yylval
.ttype
= combine_strings (strings
);
272 last_token_id
= t
->yylval
.ttype
;
274 VARRAY_FREE (strings
);
277 /* We will have always read one token too many. */
278 _cpp_backup_tokens (parse_in
, 1);
283 /* Read the next token from the input file. The token is written into
284 T, and its type number is returned. */
291 last_token
= c_lex (&last_token_id
);
292 t
->yylval
.ttype
= last_token_id
;
296 #define YYCHAR(YY) t->yychar = (YY); break;
297 #define YYCODE(C) t->yylval.code = (C);
299 case CPP_EQ
: YYCHAR('=');
300 case CPP_NOT
: YYCHAR('!');
301 case CPP_GREATER
: YYCODE(GT_EXPR
); YYCHAR('>');
302 case CPP_LESS
: YYCODE(LT_EXPR
); YYCHAR('<');
303 case CPP_PLUS
: YYCODE(PLUS_EXPR
); YYCHAR('+');
304 case CPP_MINUS
: YYCODE(MINUS_EXPR
); YYCHAR('-');
305 case CPP_MULT
: YYCODE(MULT_EXPR
); YYCHAR('*');
306 case CPP_DIV
: YYCODE(TRUNC_DIV_EXPR
); YYCHAR('/');
307 case CPP_MOD
: YYCODE(TRUNC_MOD_EXPR
); YYCHAR('%');
308 case CPP_AND
: YYCODE(BIT_AND_EXPR
); YYCHAR('&');
309 case CPP_OR
: YYCODE(BIT_IOR_EXPR
); YYCHAR('|');
310 case CPP_XOR
: YYCODE(BIT_XOR_EXPR
); YYCHAR('^');
311 case CPP_RSHIFT
: YYCODE(RSHIFT_EXPR
); YYCHAR(RSHIFT
);
312 case CPP_LSHIFT
: YYCODE(LSHIFT_EXPR
); YYCHAR(LSHIFT
);
314 case CPP_COMPL
: YYCHAR('~');
315 case CPP_AND_AND
: YYCHAR(ANDAND
);
316 case CPP_OR_OR
: YYCHAR(OROR
);
317 case CPP_QUERY
: YYCHAR('?');
318 case CPP_COLON
: YYCHAR(':');
319 case CPP_COMMA
: YYCHAR(',');
320 case CPP_OPEN_PAREN
: YYCHAR('(');
321 case CPP_CLOSE_PAREN
: YYCHAR(')');
322 case CPP_EQ_EQ
: YYCODE(EQ_EXPR
); YYCHAR(EQCOMPARE
);
323 case CPP_NOT_EQ
: YYCODE(NE_EXPR
); YYCHAR(EQCOMPARE
);
324 case CPP_GREATER_EQ
:YYCODE(GE_EXPR
); YYCHAR(ARITHCOMPARE
);
325 case CPP_LESS_EQ
: YYCODE(LE_EXPR
); YYCHAR(ARITHCOMPARE
);
327 case CPP_PLUS_EQ
: YYCODE(PLUS_EXPR
); YYCHAR(ASSIGN
);
328 case CPP_MINUS_EQ
: YYCODE(MINUS_EXPR
); YYCHAR(ASSIGN
);
329 case CPP_MULT_EQ
: YYCODE(MULT_EXPR
); YYCHAR(ASSIGN
);
330 case CPP_DIV_EQ
: YYCODE(TRUNC_DIV_EXPR
); YYCHAR(ASSIGN
);
331 case CPP_MOD_EQ
: YYCODE(TRUNC_MOD_EXPR
); YYCHAR(ASSIGN
);
332 case CPP_AND_EQ
: YYCODE(BIT_AND_EXPR
); YYCHAR(ASSIGN
);
333 case CPP_OR_EQ
: YYCODE(BIT_IOR_EXPR
); YYCHAR(ASSIGN
);
334 case CPP_XOR_EQ
: YYCODE(BIT_XOR_EXPR
); YYCHAR(ASSIGN
);
335 case CPP_RSHIFT_EQ
: YYCODE(RSHIFT_EXPR
); YYCHAR(ASSIGN
);
336 case CPP_LSHIFT_EQ
: YYCODE(LSHIFT_EXPR
); YYCHAR(ASSIGN
);
338 case CPP_OPEN_SQUARE
: YYCHAR('[');
339 case CPP_CLOSE_SQUARE
: YYCHAR(']');
340 case CPP_OPEN_BRACE
: YYCHAR('{');
341 case CPP_CLOSE_BRACE
: YYCHAR('}');
342 case CPP_SEMICOLON
: YYCHAR(';');
343 case CPP_ELLIPSIS
: YYCHAR(ELLIPSIS
);
345 case CPP_PLUS_PLUS
: YYCHAR(PLUSPLUS
);
346 case CPP_MINUS_MINUS
: YYCHAR(MINUSMINUS
);
347 case CPP_DEREF
: YYCHAR(POINTSAT
);
348 case CPP_DOT
: YYCHAR('.');
350 /* These tokens are C++ specific. */
351 case CPP_SCOPE
: YYCHAR(SCOPE
);
352 case CPP_DEREF_STAR
: YYCHAR(POINTSAT_STAR
);
353 case CPP_DOT_STAR
: YYCHAR(DOT_STAR
);
354 case CPP_MIN_EQ
: YYCODE(MIN_EXPR
); YYCHAR(ASSIGN
);
355 case CPP_MAX_EQ
: YYCODE(MAX_EXPR
); YYCHAR(ASSIGN
);
356 case CPP_MIN
: YYCODE(MIN_EXPR
); YYCHAR(MIN_MAX
);
357 case CPP_MAX
: YYCODE(MAX_EXPR
); YYCHAR(MIN_MAX
);
366 t
->yychar
= read_process_identifier (&t
->yylval
);
372 t
->yychar
= CONSTANT
;
381 yyerror ("parse error");
391 struct unparsed_text
*input
;
399 f
= obstack_alloc (&feed_obstack
, sizeof (struct feed
));
401 /* The token list starts just after the struct unparsed_text in memory. */
402 input
->pos
= (struct token
*) (input
+ 1);
406 fprintf (stderr
, "\tfeeding %s:%d [%d tokens]\n",
407 input
->filename
, input
->lineno
, input
->limit
- input
->pos
);
411 f
->filename
= input_filename
;
415 f
->first_token
= first_token
;
416 f
->token_obstack
= token_obstack
;
419 input_filename
= input
->filename
;
420 lineno
= input
->lineno
;
422 yylval
.ttype
= NULL_TREE
;
424 gcc_obstack_init (&token_obstack
);
431 struct feed
*f
= feed
;
433 input_filename
= f
->filename
;
437 first_token
= f
->first_token
;
438 obstack_free (&token_obstack
, 0);
439 token_obstack
= f
->token_obstack
;
442 obstack_free (&feed_obstack
, f
);
446 fprintf (stderr
, "\treturning to %s:%d\n", input_filename
, lineno
);
450 /* GC callback to mark memory pointed to by the pending inline queue. */
452 mark_pending_inlines (pi
)
455 struct unparsed_text
*up
= * (struct unparsed_text
**)pi
;
459 struct token
*t
= (struct token
*) (up
+ 1);
460 struct token
*l
= up
->limit
;
464 /* Some of the possible values for yychar use yylval.code
465 instead of yylval.ttype. We only have to worry about
466 yychars that could have been returned by read_token. */
469 case '+': case '-': case '*': case '/':
470 case '%': case '&': case '|': case '^':
471 case '>': case '<': case LSHIFT
: case RSHIFT
:
472 case ASSIGN
: case MIN_MAX
: case EQCOMPARE
: case ARITHCOMPARE
:
477 ggc_mark_tree (t
->yylval
.ttype
);
484 /* Token queue management. */
486 /* Return the number of tokens available on the fifo. */
487 static SPEW_INLINE
int
490 return (obstack_object_size (&token_obstack
) / sizeof (struct token
))
494 /* Fetch the token N down the line from the head of the fifo. */
496 static SPEW_INLINE
struct token
*
500 #ifdef ENABLE_CHECKING
501 /* could just have this do slurp_ implicitly, but this way is easier
503 my_friendly_assert (n
>= 0 && n
< num_tokens (), 298);
505 return ((struct token
*)obstack_base (&token_obstack
)) + n
+ first_token
;
508 static const struct token Teosi
= { END_OF_SAVED_INPUT
, 0 UNION_INIT_ZERO
};
509 static const struct token Tpad
= { EMPTY
, 0 UNION_INIT_ZERO
};
511 /* Copy the next token into T and return its value. */
512 static SPEW_INLINE
int
517 return read_token (t
);
519 if (feed
->input
->pos
< feed
->input
->limit
)
521 memcpy (t
, feed
->input
->pos
, sizeof (struct token
));
522 return (feed
->input
->pos
++)->yychar
;
525 memcpy (t
, &Teosi
, sizeof (struct token
));
526 return END_OF_SAVED_INPUT
;
529 /* Shift the next token onto the fifo. */
530 static SPEW_INLINE
int
533 size_t point
= obstack_object_size (&token_obstack
);
534 obstack_blank (&token_obstack
, sizeof (struct token
));
535 return add_token ((struct token
*) (obstack_base (&token_obstack
) + point
));
538 /* Consume the next token out of the fifo. */
540 static SPEW_INLINE
void
543 if (num_tokens () == 1)
545 obstack_free (&token_obstack
, obstack_base (&token_obstack
));
552 /* Push a token at the head of the queue; it will be the next token read. */
553 static SPEW_INLINE
void
557 if (first_token
== 0) /* We hope this doesn't happen often. */
559 size_t active
= obstack_object_size (&token_obstack
);
560 obstack_blank (&token_obstack
, sizeof (struct token
));
562 memmove (obstack_base (&token_obstack
) + sizeof (struct token
),
563 obstack_base (&token_obstack
), active
);
567 memcpy (nth_token (0), t
, sizeof (struct token
));
571 /* Pull in enough tokens that the queue is N long beyond the current
579 int num
= num_tokens ();
582 /* First, prune any empty tokens at the end. */
584 while (i
> 0 && nth_token (i
- 1)->yychar
== EMPTY
)
588 obstack_blank (&token_obstack
, -((num
- i
) * sizeof (struct token
)));
592 /* Now, if we already have enough tokens, return. */
596 /* Never read past these characters: they might separate
597 the current input stream from one we save away later. */
598 for (i
= 0; i
< num
; i
++)
600 yychar
= nth_token (i
)->yychar
;
601 if (yychar
== '{' || yychar
== ':' || yychar
== ';')
605 while (num_tokens () <= n
)
607 yychar
= shift_token ();
608 if (yychar
== '{' || yychar
== ':' || yychar
== ';')
614 while (num_tokens () <= n
)
615 obstack_grow (&token_obstack
, &Tpad
, sizeof (struct token
));
618 int looking_for_typename
;
619 int looking_for_template
;
621 static int after_friend
;
622 static int after_new
;
623 static int do_snarf_defarg
;
628 static SPEW_INLINE
int
629 identifier_type (decl
)
634 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
636 if (TREE_CODE (DECL_TEMPLATE_RESULT (decl
)) == TYPE_DECL
)
638 else if (looking_for_template
)
641 if (looking_for_template
&& really_overloaded_fn (decl
))
643 /* See through a baselink. */
644 if (TREE_CODE (decl
) == TREE_LIST
)
645 decl
= TREE_VALUE (decl
);
647 for (t
= decl
; t
!= NULL_TREE
; t
= OVL_CHAIN (t
))
648 if (DECL_FUNCTION_TEMPLATE_P (OVL_FUNCTION (t
)))
651 if (TREE_CODE (decl
) == NAMESPACE_DECL
)
653 if (TREE_CODE (decl
) != TYPE_DECL
)
655 if (DECL_ARTIFICIAL (decl
) && TREE_TYPE (decl
) == current_class_type
)
658 /* A constructor declarator for a template type will get here as an
659 implicit typename, a TYPENAME_TYPE with a type. */
661 if (t
&& TREE_CODE (t
) == TYPENAME_TYPE
)
663 decl
= TREE_TYPE (decl
);
664 if (TREE_CODE (decl
) == TYPENAME_TYPE
)
665 decl
= TREE_TYPE (decl
);
672 /* token[0] == AGGR (struct/union/enum)
673 Thus, token[1] is either a TYPENAME or a TYPENAME_DEFN.
674 If token[2] == '{' or ':' then it's TYPENAME_DEFN.
675 It's also a definition if it's a forward declaration (as in 'struct Foo;')
676 which we can tell if token[2] == ';' *and* token[-1] != FRIEND or NEW. */
678 static SPEW_INLINE
void
684 yc1
= nth_token (1)->yychar
;
685 if (yc1
!= TYPENAME
&& yc1
!= IDENTIFIER
&& yc1
!= PTYPENAME
)
687 yc2
= nth_token (2)->yychar
;
690 /* It's a forward declaration iff we were not preceded by
691 'friend' or `new'. */
692 if (after_friend
|| after_new
)
695 else if (yc2
!= '{' && yc2
!= ':')
701 nth_token (1)->yychar
= TYPENAME_DEFN
;
704 nth_token (1)->yychar
= PTYPENAME_DEFN
;
707 nth_token (1)->yychar
= IDENTIFIER_DEFN
;
717 /* Only types expected, not even namespaces. */
718 looking_for_typename
= 2;
720 if ((yychar
= yylex ()) < 0) yychar
= 0;
721 looking_for_typename
= 0;
722 if (yychar
== IDENTIFIER
)
724 lastiddecl
= lookup_name (yylval
.ttype
, -2);
726 yychar
= identifier_type (lastiddecl
);
734 int old_looking_for_typename
= 0;
735 int just_saw_new
= 0;
736 int just_saw_friend
= 0;
738 timevar_push (TV_LEX
);
745 fprintf (stderr
, "\t\t## %d @%d ", yylex_ctr
, lineno
);
752 yylval
.ttype
= snarf_defarg ();
754 got_object
= NULL_TREE
;
755 timevar_pop (TV_LEX
);
759 /* if we've got tokens, send them */
760 else if (num_tokens ())
761 yychr
= nth_token (0)->yychar
;
763 yychr
= shift_token ();
765 /* many tokens just need to be returned. At first glance, all we
766 have to do is send them back up, but some of them are needed to
767 figure out local context. */
771 /* This is a lexical no-op. */
774 debug_yychar (yychr
);
781 if (nth_token (1)->yychar
== ')')
793 peek
= nth_token (1)->yychar
;
794 yychr
= frob_id (yychr
, peek
, &nth_token (0)->yylval
.ttype
);
797 case IDENTIFIER_DEFN
:
802 /* If we see a SCOPE next, restore the old value.
803 Otherwise, we got what we want. */
804 looking_for_typename
= old_looking_for_typename
;
805 looking_for_template
= 0;
809 if (nth_token (0)->yylval
.ttype
== ridpointers
[RID_EXTERN
])
812 if (nth_token (1)->yychar
== STRING
)
814 yychr
= EXTERN_LANG_STRING
;
815 nth_token (1)->yylval
.ttype
= get_identifier
816 (TREE_STRING_POINTER (nth_token (1)->yylval
.ttype
));
820 /* do_aggr needs to know if the previous token was `friend'. */
821 else if (nth_token (0)->yylval
.ttype
== ridpointers
[RID_FRIEND
])
827 /* do_aggr needs to know if the previous token was `new'. */
835 /* If this provides a type for us, then revert lexical
836 state to standard state. */
837 looking_for_typename
= 0;
845 /* Set this again, in case we are rescanning. */
846 looking_for_typename
= 2;
853 after_friend
= just_saw_friend
;
854 after_new
= just_saw_new
;
856 /* class member lookup only applies to the first token after the object
857 expression, except for explicit destructor calls. */
859 got_object
= NULL_TREE
;
863 struct token
*tok
= nth_token (0);
865 yylval
= tok
->yylval
;
867 lineno
= tok
->lineno
;
872 debug_yychar (yychr
);
876 timevar_pop (TV_LEX
);
880 /* Unget character CH from the input stream.
881 If RESCAN is non-zero, then we want to `see' this
882 character as the next input token. */
885 yyungetc (ch
, rescan
)
889 /* Unget a character from the input stream. */
890 if (yychar
== YYEMPTY
|| rescan
== 0)
894 /* If we're putting back a brace, undo the change in indent_level
895 from the first time we saw it. */
902 fake
.yylval
.ttype
= 0;
903 fake
.lineno
= lineno
;
913 /* Lexer hackery to determine what *IDP really is. */
916 frob_id (yyc
, peek
, idp
)
922 int old_looking_for_typename
= 0;
926 /* Don't interfere with the setting from an 'aggr' prefix. */
927 old_looking_for_typename
= looking_for_typename
;
928 looking_for_typename
= 1;
930 else if (peek
== '<')
931 looking_for_template
= 1;
932 trrr
= lookup_name (*idp
, -2);
935 yyc
= identifier_type (trrr
);
942 /* If this got special lookup, remember it. In these
943 cases, we know it can't be a declarator-id. */
944 if (got_scope
|| got_object
)
956 lastiddecl
= NULL_TREE
;
957 got_scope
= NULL_TREE
;
958 looking_for_typename
= old_looking_for_typename
;
959 looking_for_template
= 0;
963 /* ID is an operator name. Duplicate the hackery in yylex to determine what
966 tree
frob_opname (id
)
970 frob_id (0, nth_token (0)->yychar
, &id
);
971 got_object
= NULL_TREE
;
975 /* Set up the state required to correctly handle the definition of the
976 inline function whose preparsed state has been saved in PI. */
979 begin_parsing_inclass_inline (pi
)
980 struct unparsed_text
*pi
;
984 /* Record that we are processing the chain of inlines starting at
987 cp_function_chain
->unparsed_inlines
= pi
;
989 processing_these_inlines
= pi
;
993 /* If this is an inline function in a local class, we must make sure
994 that we save all pertinent information about the function
995 surrounding the local class. */
996 context
= decl_function_context (pi
->decl
);
998 push_function_context_to (context
);
1001 interface_unknown
= pi
->interface
== 1;
1002 interface_only
= pi
->interface
== 0;
1003 DECL_PENDING_INLINE_P (pi
->decl
) = 0;
1004 DECL_PENDING_INLINE_INFO (pi
->decl
) = 0;
1006 /* Pass back a handle to the rest of the inline functions, so that they
1007 can be processed later. */
1008 yychar
= PRE_PARSED_FUNCTION_DECL
;
1011 start_function (NULL_TREE
, pi
->decl
, NULL_TREE
,
1012 (SF_DEFAULT
| SF_PRE_PARSED
| SF_INCLASS_INLINE
));
1015 /* Called from the top level: if there are any pending inlines to
1016 do, set up to process them now. This function sets up the first function
1017 to be parsed; after it has been, the rule for fndef in parse.y will
1018 call process_next_inline to start working on the next one. */
1021 do_pending_inlines ()
1023 /* Oops, we're still dealing with the last batch. */
1024 if (yychar
== PRE_PARSED_FUNCTION_DECL
)
1027 if (pending_inlines
)
1029 /* Clear the chain, so that any inlines nested inside the batch
1030 we're to process now don't refer to this batch. See e.g.
1031 g++.other/lookup6.C. */
1032 struct unparsed_text
*first
= pending_inlines
;
1033 pending_inlines
= pending_inlines_tail
= 0;
1035 begin_parsing_inclass_inline (first
);
1039 /* Called from the fndecl rule in the parser when the function just parsed
1040 was declared using a PRE_PARSED_FUNCTION_DECL (i.e. came from
1041 do_pending_inlines). */
1044 process_next_inline (i
)
1045 struct unparsed_text
*i
;
1047 tree decl
= i
->decl
;
1048 tree context
= decl_function_context (decl
);
1051 pop_function_context_from (context
);
1052 if (yychar
== YYEMPTY
)
1054 if (yychar
!= END_OF_SAVED_INPUT
)
1055 error ("parse error at end of saved function text");
1060 begin_parsing_inclass_inline (i
);
1064 cp_function_chain
->unparsed_inlines
= 0;
1066 processing_these_inlines
= 0;
1067 extract_interface_info ();
1072 /* Subroutine of snarf_method, deals with actual absorption of the block. */
1074 static SPEW_INLINE
void
1075 snarf_block (starting_file
, starting_line
)
1076 const char *starting_file
;
1080 int look_for_semicolon
= 0;
1081 int look_for_lbrac
= 0;
1082 int look_for_catch
= 0;
1088 /* We incremented indent_level in yylex; undo that. */
1090 else if (yychar
== '=')
1091 look_for_semicolon
= 1;
1092 else if (yychar
== ':' || yychar
== RETURN_KEYWORD
|| yychar
== TRY
)
1100 yyerror ("parse error in method specification");
1102 /* The current token is the first one to be recorded. */
1103 tmp
.yychar
= yychar
;
1104 tmp
.yylval
= yylval
;
1105 tmp
.lineno
= lineno
;
1106 obstack_grow (&inline_text_obstack
, &tmp
, sizeof (struct token
));
1110 point
= obstack_object_size (&inline_text_obstack
);
1111 obstack_blank (&inline_text_obstack
, sizeof (struct token
));
1112 yyc
= add_token ((struct token
*)
1113 (obstack_base (&inline_text_obstack
) + point
));
1120 else if (yyc
== '}')
1123 if (blev
== 0 && !look_for_semicolon
)
1125 if (!look_for_catch
)
1128 if (add_token (&tmp
) != CATCH
)
1135 obstack_grow (&inline_text_obstack
, &tmp
, sizeof (struct token
));
1138 else if (yyc
== ';')
1142 error ("function body for constructor missing");
1143 /* fake a { } to avoid further errors */
1144 tmp
.yylval
.ttype
= 0;
1146 obstack_grow (&inline_text_obstack
, &tmp
, sizeof (struct token
));
1148 obstack_grow (&inline_text_obstack
, &tmp
, sizeof (struct token
));
1151 else if (look_for_semicolon
&& blev
== 0)
1156 error_with_file_and_line (starting_file
, starting_line
,
1157 "end of file read inside definition");
1163 /* This function stores away the text for an inline function that should
1164 be processed later (by do_pending_inlines). */
1169 int starting_lineno
= lineno
;
1170 const char *starting_filename
= input_filename
;
1173 struct unparsed_text
*meth
;
1175 /* Leave room for the header, then absorb the block. */
1176 obstack_blank (&inline_text_obstack
, sizeof (struct unparsed_text
));
1177 snarf_block (starting_filename
, starting_lineno
);
1179 len
= obstack_object_size (&inline_text_obstack
);
1180 meth
= (struct unparsed_text
*) obstack_finish (&inline_text_obstack
);
1182 /* Happens when we get two declarations of the same function in the
1184 if (decl
== void_type_node
1185 || (current_class_type
&& TYPE_REDEFINED (current_class_type
)))
1187 obstack_free (&inline_text_obstack
, (char *)meth
);
1192 meth
->filename
= starting_filename
;
1193 meth
->lineno
= starting_lineno
;
1194 meth
->limit
= (struct token
*) ((char *)meth
+ len
);
1195 meth
->interface
= (interface_unknown
? 1 : (interface_only
? 0 : 2));
1200 fprintf (stderr
, "\tsaved method of %d tokens from %s:%d\n",
1201 meth
->limit
- (struct token
*) (meth
+ 1),
1202 starting_filename
, starting_lineno
);
1205 DECL_PENDING_INLINE_INFO (decl
) = meth
;
1206 DECL_PENDING_INLINE_P (decl
) = 1;
1208 if (pending_inlines_tail
)
1209 pending_inlines_tail
->next
= meth
;
1211 pending_inlines
= meth
;
1212 pending_inlines_tail
= meth
;
1215 /* Consume a no-commas expression - a default argument - and save it
1216 on the inline_text_obstack. */
1221 int starting_lineno
= lineno
;
1222 const char *starting_filename
= input_filename
;
1227 struct unparsed_text
*buf
;
1230 obstack_blank (&inline_text_obstack
, sizeof (struct unparsed_text
));
1234 point
= obstack_object_size (&inline_text_obstack
);
1235 obstack_blank (&inline_text_obstack
, sizeof (struct token
));
1236 yyc
= add_token ((struct token
*)
1237 (obstack_base (&inline_text_obstack
) + point
));
1239 if (plev
<= 0 && (yyc
== ')' || yyc
== ','))
1241 else if (yyc
== '(' || yyc
== '[')
1243 else if (yyc
== ']' || yyc
== ')')
1247 error_with_file_and_line (starting_filename
, starting_lineno
,
1248 "end of file read inside default argument");
1253 /* Unget the last token. */
1254 push_token ((struct token
*) (obstack_base (&inline_text_obstack
) + point
));
1255 /* This is the documented way to shrink a growing obstack block. */
1256 obstack_blank (&inline_text_obstack
, - (int) sizeof (struct token
));
1259 len
= obstack_object_size (&inline_text_obstack
);
1260 buf
= (struct unparsed_text
*) obstack_finish (&inline_text_obstack
);
1263 buf
->filename
= starting_filename
;
1264 buf
->lineno
= starting_lineno
;
1265 buf
->limit
= (struct token
*) ((char *)buf
+ len
);
1270 fprintf (stderr
, "\tsaved defarg of %d tokens from %s:%d\n",
1271 buf
->limit
- (struct token
*) (buf
+ 1),
1272 starting_filename
, starting_lineno
);
1275 arg
= make_node (DEFAULT_ARG
);
1276 DEFARG_POINTER (arg
) = (char *)buf
;
1281 /* Decide whether the default argument we are about to see should be
1282 gobbled up as text for later parsing. */
1285 maybe_snarf_defarg ()
1287 if (current_class_type
&& TYPE_BEING_DEFINED (current_class_type
))
1288 do_snarf_defarg
= 1;
1291 /* Called from grokfndecl to note a function decl with unparsed default
1292 arguments for later processing. Also called from grokdeclarator
1293 for function types with unparsed defargs; the call from grokfndecl
1294 will always come second, so we can overwrite the entry from the type. */
1297 add_defarg_fn (decl
)
1300 if (TREE_CODE (decl
) == FUNCTION_DECL
)
1301 TREE_VALUE (defarg_fns
) = decl
;
1304 defarg_fns
= tree_cons (NULL_TREE
, decl
, defarg_fns
);
1305 TREE_TYPE (defarg_fns
) = current_class_type
;
1309 /* Helper for do_pending_defargs. Starts the parsing of a default arg. */
1315 tree d
= TREE_PURPOSE (p
);
1317 feed_input ((struct unparsed_text
*)DEFARG_POINTER (d
));
1318 yychar
= DEFARG_MARKER
;
1322 /* Helper for do_pending_defargs. Ends the parsing of a default arg. */
1327 if (yychar
== YYEMPTY
)
1329 if (yychar
!= END_OF_SAVED_INPUT
)
1330 error ("parse error at end of saved function text");
1335 /* Main function for deferred parsing of default arguments. Called from
1339 do_pending_defargs ()
1346 tree current
= defarg_fns
;
1348 tree defarg_fn
= TREE_VALUE (defarg_fns
);
1349 if (defarg_parm
== NULL_TREE
)
1351 push_nested_class (TREE_TYPE (defarg_fns
), 1);
1353 if (TREE_CODE (defarg_fn
) == FUNCTION_DECL
)
1354 maybe_begin_member_template_processing (defarg_fn
);
1356 if (TREE_CODE (defarg_fn
) == FUNCTION_DECL
)
1357 defarg_parm
= TYPE_ARG_TYPES (TREE_TYPE (defarg_fn
));
1359 defarg_parm
= TYPE_ARG_TYPES (defarg_fn
);
1362 defarg_parm
= TREE_CHAIN (defarg_parm
);
1364 for (; defarg_parm
; defarg_parm
= TREE_CHAIN (defarg_parm
))
1365 if (!TREE_PURPOSE (defarg_parm
)
1366 || TREE_CODE (TREE_PURPOSE (defarg_parm
)) != DEFAULT_ARG
)
1368 else if (TREE_PURPOSE (current
) == error_mark_node
)
1369 DEFARG_POINTER (TREE_PURPOSE (defarg_parm
)) = NULL
;
1372 feed_defarg (defarg_parm
);
1374 /* Return to the parser, which will process this defarg
1375 and call us again. */
1379 if (TREE_CODE (defarg_fn
) == FUNCTION_DECL
)
1381 maybe_end_member_template_processing ();
1382 check_default_args (defarg_fn
);
1386 pop_nested_class ();
1388 defarg_fns
= TREE_CHAIN (defarg_fns
);
1391 /* This function's default args depend on unprocessed default args
1392 of defarg_fns. We will need to reprocess this function, and
1393 check for circular dependencies. */
1396 for (a
= defarg_depfns
, b
= TREE_PURPOSE (current
); a
&& b
;
1397 a
= TREE_CHAIN (a
), b
= TREE_CHAIN (b
))
1398 if (TREE_VALUE (a
) != TREE_VALUE (b
))
1403 TREE_CHAIN (current
) = NULL_TREE
;
1404 defarg_fns
= chainon (defarg_fns
, current
);
1405 TREE_PURPOSE (current
) = defarg_depfns
;
1409 cp_warning_at ("circular dependency in default args of `%#D'", defarg_fn
);
1410 /* No need to say what else is dependent, as they will be
1411 picked up in another pass. */
1413 /* Immediately repeat, but marked so that we break the loop. */
1414 defarg_fns
= current
;
1415 TREE_PURPOSE (current
) = error_mark_node
;
1417 defarg_depfns
= NULL_TREE
;
1419 else if (TREE_PURPOSE (current
) == error_mark_node
)
1420 defarg_fnsdone
= tree_cons (NULL_TREE
, defarg_fn
, defarg_fnsdone
);
1424 /* After parsing all the default arguments, we must clear any that remain,
1425 which will be part of a circular dependency. */
1427 done_pending_defargs ()
1429 for (; defarg_fnsdone
; defarg_fnsdone
= TREE_CHAIN (defarg_fnsdone
))
1431 tree fn
= TREE_VALUE (defarg_fnsdone
);
1434 if (TREE_CODE (fn
) == FUNCTION_DECL
)
1435 parms
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
1437 parms
= TYPE_ARG_TYPES (fn
);
1438 for (; parms
; parms
= TREE_CHAIN (parms
))
1439 if (TREE_PURPOSE (parms
)
1440 && TREE_CODE (TREE_PURPOSE (parms
)) == DEFAULT_ARG
)
1442 my_friendly_assert (!DEFARG_POINTER (TREE_PURPOSE (parms
)), 20010107);
1443 TREE_PURPOSE (parms
) = NULL_TREE
;
1448 /* In processing the current default arg, we called FN, but that call
1449 required a default argument of FN, and that had not yet been processed.
1453 unprocessed_defarg_fn (fn
)
1456 defarg_depfns
= tree_cons (NULL_TREE
, fn
, defarg_depfns
);
1459 /* Called from the parser to update an element of TYPE_ARG_TYPES for some
1460 FUNCTION_TYPE with the newly parsed version of its default argument, which
1461 was previously digested as text. */
1464 replace_defarg (arg
, init
)
1467 if (init
== error_mark_node
)
1468 TREE_PURPOSE (arg
) = error_mark_node
;
1471 if (! processing_template_decl
1472 && ! can_convert_arg (TREE_VALUE (arg
), TREE_TYPE (init
), init
))
1473 pedwarn ("invalid type `%T' for default argument to `%T'",
1474 TREE_TYPE (init
), TREE_VALUE (arg
));
1476 TREE_PURPOSE (arg
) = init
;
1481 /* debug_yychar takes a yychar (token number) value and prints its name. */
1488 fprintf (stderr
, "->%d < %c >\n", lineno
, yy
);
1489 else if (yy
== IDENTIFIER
|| yy
== TYPENAME
)
1492 if (TREE_CODE (yylval
.ttype
) == IDENTIFIER_NODE
)
1493 id
= IDENTIFIER_POINTER (yylval
.ttype
);
1494 else if (TREE_CODE_CLASS (TREE_CODE (yylval
.ttype
)) == 'd')
1495 id
= IDENTIFIER_POINTER (DECL_NAME (yylval
.ttype
));
1498 fprintf (stderr
, "->%d <%s `%s'>\n", lineno
, debug_yytranslate (yy
), id
);
1501 fprintf (stderr
, "->%d <%s>\n", lineno
, debug_yytranslate (yy
));
1506 #define NAME(TYPE) cpp_type2name (TYPE)
1512 const char *string
= _(msgid
);
1514 if (last_token
== CPP_EOF
)
1515 error ("%s at end of input", string
);
1516 else if (last_token
== CPP_CHAR
|| last_token
== CPP_WCHAR
)
1518 unsigned int val
= TREE_INT_CST_LOW (yylval
.ttype
);
1519 const char *const ell
= (last_token
== CPP_CHAR
) ? "" : "L";
1520 if (val
<= UCHAR_MAX
&& ISGRAPH (val
))
1521 error ("%s before %s'%c'", string
, ell
, val
);
1523 error ("%s before %s'\\x%x'", string
, ell
, val
);
1525 else if (last_token
== CPP_STRING
1526 || last_token
== CPP_WSTRING
)
1527 error ("%s before string constant", string
);
1528 else if (last_token
== CPP_NUMBER
)
1529 error ("%s before numeric constant", string
);
1530 else if (last_token
== CPP_NAME
)
1532 if (TREE_CODE (last_token_id
) == IDENTIFIER_NODE
)
1533 error ("%s before `%s'", string
, IDENTIFIER_POINTER (last_token_id
));
1534 else if (ISGRAPH (yychar
))
1535 error ("%s before `%c'", string
, yychar
);
1537 error ("%s before `\%o'", string
, yychar
);
1540 error ("%s before `%s' token", string
, NAME (last_token
));