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 int read_token
PARAMS ((struct token
*));
107 static SPEW_INLINE
int num_tokens
PARAMS ((void));
108 static SPEW_INLINE
struct token
*nth_token
PARAMS ((int));
109 static SPEW_INLINE
int add_token
PARAMS ((struct token
*));
110 static SPEW_INLINE
int shift_token
PARAMS ((void));
111 static SPEW_INLINE
void push_token
PARAMS ((struct token
*));
112 static SPEW_INLINE
void consume_token
PARAMS ((void));
113 static SPEW_INLINE
int read_process_identifier
PARAMS ((YYSTYPE
*));
115 static SPEW_INLINE
void feed_input
PARAMS ((struct unparsed_text
*));
116 static SPEW_INLINE
void snarf_block
PARAMS ((const char *, int));
117 static tree snarf_defarg
PARAMS ((void));
118 static int frob_id
PARAMS ((int, int, tree
*));
120 /* The list of inline functions being held off until we reach the end of
121 the current class declaration. */
122 struct unparsed_text
*pending_inlines
;
123 struct unparsed_text
*pending_inlines_tail
;
125 /* The list of previously-deferred inline functions currently being parsed.
126 This exists solely to be a GC root. */
127 struct unparsed_text
*processing_these_inlines
;
129 static void begin_parsing_inclass_inline
PARAMS ((struct unparsed_text
*));
130 static void mark_pending_inlines
PARAMS ((PTR
));
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 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 == '$'
241 || VTABLE_NAME_P (id
)
243 || ANON_AGGRNAME_P (id
))
245 "identifier name `%s' conflicts with GNU C++ internal naming strategy",
246 IDENTIFIER_POINTER (id
));
251 /* Read the next token from the input file. The token is written into
252 T, and its type number is returned. */
259 last_token
= c_lex (&last_token_id
);
260 t
->yylval
.ttype
= last_token_id
;
264 #define YYCHAR(YY) t->yychar = (YY); break;
265 #define YYCODE(C) t->yylval.code = (C);
267 case CPP_EQ
: YYCHAR('=');
268 case CPP_NOT
: YYCHAR('!');
269 case CPP_GREATER
: YYCODE(GT_EXPR
); YYCHAR('>');
270 case CPP_LESS
: YYCODE(LT_EXPR
); YYCHAR('<');
271 case CPP_PLUS
: YYCODE(PLUS_EXPR
); YYCHAR('+');
272 case CPP_MINUS
: YYCODE(MINUS_EXPR
); YYCHAR('-');
273 case CPP_MULT
: YYCODE(MULT_EXPR
); YYCHAR('*');
274 case CPP_DIV
: YYCODE(TRUNC_DIV_EXPR
); YYCHAR('/');
275 case CPP_MOD
: YYCODE(TRUNC_MOD_EXPR
); YYCHAR('%');
276 case CPP_AND
: YYCODE(BIT_AND_EXPR
); YYCHAR('&');
277 case CPP_OR
: YYCODE(BIT_IOR_EXPR
); YYCHAR('|');
278 case CPP_XOR
: YYCODE(BIT_XOR_EXPR
); YYCHAR('^');
279 case CPP_RSHIFT
: YYCODE(RSHIFT_EXPR
); YYCHAR(RSHIFT
);
280 case CPP_LSHIFT
: YYCODE(LSHIFT_EXPR
); YYCHAR(LSHIFT
);
282 case CPP_COMPL
: YYCHAR('~');
283 case CPP_AND_AND
: YYCHAR(ANDAND
);
284 case CPP_OR_OR
: YYCHAR(OROR
);
285 case CPP_QUERY
: YYCHAR('?');
286 case CPP_COLON
: YYCHAR(':');
287 case CPP_COMMA
: YYCHAR(',');
288 case CPP_OPEN_PAREN
: YYCHAR('(');
289 case CPP_CLOSE_PAREN
: YYCHAR(')');
290 case CPP_EQ_EQ
: YYCODE(EQ_EXPR
); YYCHAR(EQCOMPARE
);
291 case CPP_NOT_EQ
: YYCODE(NE_EXPR
); YYCHAR(EQCOMPARE
);
292 case CPP_GREATER_EQ
:YYCODE(GE_EXPR
); YYCHAR(ARITHCOMPARE
);
293 case CPP_LESS_EQ
: YYCODE(LE_EXPR
); YYCHAR(ARITHCOMPARE
);
295 case CPP_PLUS_EQ
: YYCODE(PLUS_EXPR
); YYCHAR(ASSIGN
);
296 case CPP_MINUS_EQ
: YYCODE(MINUS_EXPR
); YYCHAR(ASSIGN
);
297 case CPP_MULT_EQ
: YYCODE(MULT_EXPR
); YYCHAR(ASSIGN
);
298 case CPP_DIV_EQ
: YYCODE(TRUNC_DIV_EXPR
); YYCHAR(ASSIGN
);
299 case CPP_MOD_EQ
: YYCODE(TRUNC_MOD_EXPR
); YYCHAR(ASSIGN
);
300 case CPP_AND_EQ
: YYCODE(BIT_AND_EXPR
); YYCHAR(ASSIGN
);
301 case CPP_OR_EQ
: YYCODE(BIT_IOR_EXPR
); YYCHAR(ASSIGN
);
302 case CPP_XOR_EQ
: YYCODE(BIT_XOR_EXPR
); YYCHAR(ASSIGN
);
303 case CPP_RSHIFT_EQ
: YYCODE(RSHIFT_EXPR
); YYCHAR(ASSIGN
);
304 case CPP_LSHIFT_EQ
: YYCODE(LSHIFT_EXPR
); YYCHAR(ASSIGN
);
306 case CPP_OPEN_SQUARE
: YYCHAR('[');
307 case CPP_CLOSE_SQUARE
: YYCHAR(']');
308 case CPP_OPEN_BRACE
: YYCHAR('{');
309 case CPP_CLOSE_BRACE
: YYCHAR('}');
310 case CPP_SEMICOLON
: YYCHAR(';');
311 case CPP_ELLIPSIS
: YYCHAR(ELLIPSIS
);
313 case CPP_PLUS_PLUS
: YYCHAR(PLUSPLUS
);
314 case CPP_MINUS_MINUS
: YYCHAR(MINUSMINUS
);
315 case CPP_DEREF
: YYCHAR(POINTSAT
);
316 case CPP_DOT
: YYCHAR('.');
318 /* These tokens are C++ specific. */
319 case CPP_SCOPE
: YYCHAR(SCOPE
);
320 case CPP_DEREF_STAR
: YYCHAR(POINTSAT_STAR
);
321 case CPP_DOT_STAR
: YYCHAR(DOT_STAR
);
322 case CPP_MIN_EQ
: YYCODE(MIN_EXPR
); YYCHAR(ASSIGN
);
323 case CPP_MAX_EQ
: YYCODE(MAX_EXPR
); YYCHAR(ASSIGN
);
324 case CPP_MIN
: YYCODE(MIN_EXPR
); YYCHAR(MIN_MAX
);
325 case CPP_MAX
: YYCODE(MAX_EXPR
); YYCHAR(MIN_MAX
);
334 t
->yychar
= read_process_identifier (&t
->yylval
);
340 t
->yychar
= CONSTANT
;
349 yyerror ("parse error");
359 struct unparsed_text
*input
;
367 f
= obstack_alloc (&feed_obstack
, sizeof (struct feed
));
369 /* The token list starts just after the struct unparsed_text in memory. */
370 input
->pos
= (struct token
*) (input
+ 1);
374 fprintf (stderr
, "\tfeeding %s:%d [%d tokens]\n",
375 input
->filename
, input
->lineno
, input
->limit
- input
->pos
);
379 f
->filename
= input_filename
;
383 f
->first_token
= first_token
;
384 f
->token_obstack
= token_obstack
;
387 input_filename
= input
->filename
;
388 lineno
= input
->lineno
;
390 yylval
.ttype
= NULL_TREE
;
392 gcc_obstack_init (&token_obstack
);
399 struct feed
*f
= feed
;
401 input_filename
= f
->filename
;
405 first_token
= f
->first_token
;
406 obstack_free (&token_obstack
, 0);
407 token_obstack
= f
->token_obstack
;
410 obstack_free (&feed_obstack
, f
);
414 fprintf (stderr
, "\treturning to %s:%d\n", input_filename
, lineno
);
418 /* GC callback to mark memory pointed to by the pending inline queue. */
420 mark_pending_inlines (pi
)
423 struct unparsed_text
*up
= * (struct unparsed_text
**)pi
;
427 struct token
*t
= (struct token
*) (up
+ 1);
428 struct token
*l
= up
->limit
;
432 /* Some of the possible values for yychar use yylval.code
433 instead of yylval.ttype. We only have to worry about
434 yychars that could have been returned by read_token. */
437 case '+': case '-': case '*': case '/':
438 case '%': case '&': case '|': case '^':
439 case '>': case '<': case LSHIFT
: case RSHIFT
:
440 case ASSIGN
: case MIN_MAX
: case EQCOMPARE
: case ARITHCOMPARE
:
445 ggc_mark_tree (t
->yylval
.ttype
);
452 /* Token queue management. */
454 /* Return the number of tokens available on the fifo. */
455 static SPEW_INLINE
int
458 return (obstack_object_size (&token_obstack
) / sizeof (struct token
))
462 /* Fetch the token N down the line from the head of the fifo. */
464 static SPEW_INLINE
struct token
*
468 #ifdef ENABLE_CHECKING
469 /* could just have this do slurp_ implicitly, but this way is easier
471 my_friendly_assert (n
>= 0 && n
< num_tokens (), 298);
473 return ((struct token
*)obstack_base (&token_obstack
)) + n
+ first_token
;
476 static const struct token Teosi
= { END_OF_SAVED_INPUT
, 0 UNION_INIT_ZERO
};
477 static const struct token Tpad
= { EMPTY
, 0 UNION_INIT_ZERO
};
479 /* Copy the next token into T and return its value. */
480 static SPEW_INLINE
int
485 return read_token (t
);
487 if (feed
->input
->pos
< feed
->input
->limit
)
489 memcpy (t
, feed
->input
->pos
, sizeof (struct token
));
490 return (feed
->input
->pos
++)->yychar
;
493 memcpy (t
, &Teosi
, sizeof (struct token
));
494 return END_OF_SAVED_INPUT
;
497 /* Shift the next token onto the fifo. */
498 static SPEW_INLINE
int
501 size_t point
= obstack_object_size (&token_obstack
);
502 obstack_blank (&token_obstack
, sizeof (struct token
));
503 return add_token ((struct token
*) (obstack_base (&token_obstack
) + point
));
506 /* Consume the next token out of the fifo. */
508 static SPEW_INLINE
void
511 if (num_tokens () == 1)
513 obstack_free (&token_obstack
, obstack_base (&token_obstack
));
520 /* Push a token at the head of the queue; it will be the next token read. */
521 static SPEW_INLINE
void
525 if (first_token
== 0) /* We hope this doesn't happen often. */
527 size_t active
= obstack_object_size (&token_obstack
);
528 obstack_blank (&token_obstack
, sizeof (struct token
));
530 memmove (obstack_base (&token_obstack
) + sizeof (struct token
),
531 obstack_base (&token_obstack
), active
);
535 memcpy (nth_token (0), t
, sizeof (struct token
));
539 /* Pull in enough tokens that the queue is N long beyond the current
547 int num
= num_tokens ();
550 /* First, prune any empty tokens at the end. */
552 while (i
> 0 && nth_token (i
- 1)->yychar
== EMPTY
)
556 obstack_blank (&token_obstack
, -((num
- i
) * sizeof (struct token
)));
560 /* Now, if we already have enough tokens, return. */
564 /* Never read past these characters: they might separate
565 the current input stream from one we save away later. */
566 for (i
= 0; i
< num
; i
++)
568 yychar
= nth_token (i
)->yychar
;
569 if (yychar
== '{' || yychar
== ':' || yychar
== ';')
573 while (num_tokens () <= n
)
575 yychar
= shift_token ();
576 if (yychar
== '{' || yychar
== ':' || yychar
== ';')
582 while (num_tokens () <= n
)
583 obstack_grow (&token_obstack
, &Tpad
, sizeof (struct token
));
586 int looking_for_typename
;
587 int looking_for_template
;
589 static int after_friend
;
590 static int after_new
;
591 static int do_snarf_defarg
;
596 static SPEW_INLINE
int
597 identifier_type (decl
)
602 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
604 if (TREE_CODE (DECL_TEMPLATE_RESULT (decl
)) == TYPE_DECL
)
606 else if (looking_for_template
)
609 if (looking_for_template
&& really_overloaded_fn (decl
))
611 /* See through a baselink. */
612 if (TREE_CODE (decl
) == TREE_LIST
)
613 decl
= TREE_VALUE (decl
);
615 for (t
= decl
; t
!= NULL_TREE
; t
= OVL_CHAIN (t
))
616 if (DECL_FUNCTION_TEMPLATE_P (OVL_FUNCTION (t
)))
619 if (TREE_CODE (decl
) == NAMESPACE_DECL
)
621 if (TREE_CODE (decl
) != TYPE_DECL
)
623 if (DECL_ARTIFICIAL (decl
) && TREE_TYPE (decl
) == current_class_type
)
626 /* A constructor declarator for a template type will get here as an
627 implicit typename, a TYPENAME_TYPE with a type. */
629 if (t
&& TREE_CODE (t
) == TYPENAME_TYPE
)
631 decl
= TREE_TYPE (decl
);
632 if (TREE_CODE (decl
) == TYPENAME_TYPE
)
633 decl
= TREE_TYPE (decl
);
640 /* token[0] == AGGR (struct/union/enum)
641 Thus, token[1] is either a TYPENAME or a TYPENAME_DEFN.
642 If token[2] == '{' or ':' then it's TYPENAME_DEFN.
643 It's also a definition if it's a forward declaration (as in 'struct Foo;')
644 which we can tell if token[2] == ';' *and* token[-1] != FRIEND or NEW. */
646 static SPEW_INLINE
void
652 yc1
= nth_token (1)->yychar
;
653 if (yc1
!= TYPENAME
&& yc1
!= IDENTIFIER
&& yc1
!= PTYPENAME
)
655 yc2
= nth_token (2)->yychar
;
658 /* It's a forward declaration iff we were not preceded by
659 'friend' or `new'. */
660 if (after_friend
|| after_new
)
663 else if (yc2
!= '{' && yc2
!= ':')
669 nth_token (1)->yychar
= TYPENAME_DEFN
;
672 nth_token (1)->yychar
= PTYPENAME_DEFN
;
675 nth_token (1)->yychar
= IDENTIFIER_DEFN
;
678 my_friendly_abort (102);
685 /* Only types expected, not even namespaces. */
686 looking_for_typename
= 2;
688 if ((yychar
= yylex ()) < 0) yychar
= 0;
689 looking_for_typename
= 0;
690 if (yychar
== IDENTIFIER
)
692 lastiddecl
= lookup_name (yylval
.ttype
, -2);
694 yychar
= identifier_type (lastiddecl
);
702 int old_looking_for_typename
= 0;
703 int just_saw_new
= 0;
704 int just_saw_friend
= 0;
706 timevar_push (TV_LEX
);
713 fprintf (stderr
, "\t\t## %d @%d ", yylex_ctr
, lineno
);
720 yylval
.ttype
= snarf_defarg ();
722 got_object
= NULL_TREE
;
723 timevar_pop (TV_LEX
);
727 /* if we've got tokens, send them */
728 else if (num_tokens ())
729 yychr
= nth_token (0)->yychar
;
731 yychr
= shift_token ();
733 /* many tokens just need to be returned. At first glance, all we
734 have to do is send them back up, but some of them are needed to
735 figure out local context. */
739 /* This is a lexical no-op. */
742 debug_yychar (yychr
);
749 if (nth_token (1)->yychar
== ')')
761 peek
= nth_token (1)->yychar
;
762 yychr
= frob_id (yychr
, peek
, &nth_token (0)->yylval
.ttype
);
765 case IDENTIFIER_DEFN
:
770 /* If we see a SCOPE next, restore the old value.
771 Otherwise, we got what we want. */
772 looking_for_typename
= old_looking_for_typename
;
773 looking_for_template
= 0;
777 if (nth_token (0)->yylval
.ttype
== ridpointers
[RID_EXTERN
])
780 if (nth_token (1)->yychar
== STRING
)
782 yychr
= EXTERN_LANG_STRING
;
783 nth_token (1)->yylval
.ttype
= get_identifier
784 (TREE_STRING_POINTER (nth_token (1)->yylval
.ttype
));
788 /* do_aggr needs to know if the previous token was `friend'. */
789 else if (nth_token (0)->yylval
.ttype
== ridpointers
[RID_FRIEND
])
795 /* do_aggr needs to know if the previous token was `new'. */
803 /* If this provides a type for us, then revert lexical
804 state to standard state. */
805 looking_for_typename
= 0;
813 /* Set this again, in case we are rescanning. */
814 looking_for_typename
= 2;
821 after_friend
= just_saw_friend
;
822 after_new
= just_saw_new
;
824 /* class member lookup only applies to the first token after the object
825 expression, except for explicit destructor calls. */
827 got_object
= NULL_TREE
;
831 struct token
*tok
= nth_token (0);
833 yylval
= tok
->yylval
;
835 lineno
= tok
->lineno
;
840 debug_yychar (yychr
);
844 timevar_pop (TV_LEX
);
848 /* Unget character CH from the input stream.
849 If RESCAN is non-zero, then we want to `see' this
850 character as the next input token. */
853 yyungetc (ch
, rescan
)
857 /* Unget a character from the input stream. */
858 if (yychar
== YYEMPTY
|| rescan
== 0)
862 /* If we're putting back a brace, undo the change in indent_level
863 from the first time we saw it. */
870 fake
.yylval
.ttype
= 0;
871 fake
.lineno
= lineno
;
881 /* Lexer hackery to determine what *IDP really is. */
884 frob_id (yyc
, peek
, idp
)
890 int old_looking_for_typename
= 0;
894 /* Don't interfere with the setting from an 'aggr' prefix. */
895 old_looking_for_typename
= looking_for_typename
;
896 looking_for_typename
= 1;
898 else if (peek
== '<')
899 looking_for_template
= 1;
900 trrr
= lookup_name (*idp
, -2);
903 yyc
= identifier_type (trrr
);
910 /* If this got special lookup, remember it. In these
911 cases, we know it can't be a declarator-id. */
912 if (got_scope
|| got_object
)
920 my_friendly_abort (20000907);
924 lastiddecl
= NULL_TREE
;
925 got_scope
= NULL_TREE
;
926 looking_for_typename
= old_looking_for_typename
;
927 looking_for_template
= 0;
931 /* ID is an operator name. Duplicate the hackery in yylex to determine what
934 tree
frob_opname (id
)
938 frob_id (0, nth_token (0)->yychar
, &id
);
939 got_object
= NULL_TREE
;
943 /* Set up the state required to correctly handle the definition of the
944 inline function whose preparsed state has been saved in PI. */
947 begin_parsing_inclass_inline (pi
)
948 struct unparsed_text
*pi
;
952 /* Record that we are processing the chain of inlines starting at
953 PI in a special GC root. */
954 processing_these_inlines
= pi
;
958 /* If this is an inline function in a local class, we must make sure
959 that we save all pertinent information about the function
960 surrounding the local class. */
961 context
= decl_function_context (pi
->decl
);
963 push_function_context_to (context
);
966 interface_unknown
= pi
->interface
== 1;
967 interface_only
= pi
->interface
== 0;
968 DECL_PENDING_INLINE_P (pi
->decl
) = 0;
969 DECL_PENDING_INLINE_INFO (pi
->decl
) = 0;
971 /* Pass back a handle to the rest of the inline functions, so that they
972 can be processed later. */
973 yychar
= PRE_PARSED_FUNCTION_DECL
;
976 start_function (NULL_TREE
, pi
->decl
, NULL_TREE
,
977 (SF_DEFAULT
| SF_PRE_PARSED
| SF_INCLASS_INLINE
));
980 /* Called from the top level: if there are any pending inlines to
981 do, set up to process them now. This function sets up the first function
982 to be parsed; after it has been, the rule for fndef in parse.y will
983 call process_next_inline to start working on the next one. */
986 do_pending_inlines ()
988 /* Oops, we're still dealing with the last batch. */
989 if (yychar
== PRE_PARSED_FUNCTION_DECL
)
994 /* Clear the chain, so that any inlines nested inside the batch
995 we're to process now don't refer to this batch. See e.g.
996 g++.other/lookup6.C. */
997 struct unparsed_text
*first
= pending_inlines
;
998 pending_inlines
= pending_inlines_tail
= 0;
1000 begin_parsing_inclass_inline (first
);
1004 /* Called from the fndecl rule in the parser when the function just parsed
1005 was declared using a PRE_PARSED_FUNCTION_DECL (i.e. came from
1006 do_pending_inlines). */
1009 process_next_inline (i
)
1010 struct unparsed_text
*i
;
1012 tree decl
= i
->decl
;
1013 tree context
= decl_function_context (decl
);
1016 pop_function_context_from (context
);
1017 if (yychar
== YYEMPTY
)
1019 if (yychar
!= END_OF_SAVED_INPUT
)
1020 error ("parse error at end of saved function text");
1025 begin_parsing_inclass_inline (i
);
1028 processing_these_inlines
= 0;
1029 extract_interface_info ();
1034 /* Subroutine of snarf_method, deals with actual absorption of the block. */
1036 static SPEW_INLINE
void
1037 snarf_block (starting_file
, starting_line
)
1038 const char *starting_file
;
1042 int look_for_semicolon
= 0;
1043 int look_for_lbrac
= 0;
1044 int look_for_catch
= 0;
1050 /* We incremented indent_level in yylex; undo that. */
1052 else if (yychar
== '=')
1053 look_for_semicolon
= 1;
1054 else if (yychar
== ':' || yychar
== RETURN_KEYWORD
|| yychar
== TRY
)
1062 yyerror ("parse error in method specification");
1064 /* The current token is the first one to be recorded. */
1065 tmp
.yychar
= yychar
;
1066 tmp
.yylval
= yylval
;
1067 tmp
.lineno
= lineno
;
1068 obstack_grow (&inline_text_obstack
, &tmp
, sizeof (struct token
));
1072 point
= obstack_object_size (&inline_text_obstack
);
1073 obstack_blank (&inline_text_obstack
, sizeof (struct token
));
1074 yyc
= add_token ((struct token
*)
1075 (obstack_base (&inline_text_obstack
) + point
));
1082 else if (yyc
== '}')
1085 if (blev
== 0 && !look_for_semicolon
)
1087 if (!look_for_catch
)
1090 if (add_token (&tmp
) != CATCH
)
1097 obstack_grow (&inline_text_obstack
, &tmp
, sizeof (struct token
));
1100 else if (yyc
== ';')
1104 error ("function body for constructor missing");
1105 /* fake a { } to avoid further errors */
1106 tmp
.yylval
.ttype
= 0;
1108 obstack_grow (&inline_text_obstack
, &tmp
, sizeof (struct token
));
1110 obstack_grow (&inline_text_obstack
, &tmp
, sizeof (struct token
));
1113 else if (look_for_semicolon
&& blev
== 0)
1118 error_with_file_and_line (starting_file
, starting_line
,
1119 "end of file read inside definition");
1125 /* This function stores away the text for an inline function that should
1126 be processed later (by do_pending_inlines). */
1131 int starting_lineno
= lineno
;
1132 const char *starting_filename
= input_filename
;
1135 struct unparsed_text
*meth
;
1137 /* Leave room for the header, then absorb the block. */
1138 obstack_blank (&inline_text_obstack
, sizeof (struct unparsed_text
));
1139 snarf_block (starting_filename
, starting_lineno
);
1141 len
= obstack_object_size (&inline_text_obstack
);
1142 meth
= (struct unparsed_text
*) obstack_finish (&inline_text_obstack
);
1144 /* Happens when we get two declarations of the same function in the
1146 if (decl
== void_type_node
1147 || (current_class_type
&& TYPE_REDEFINED (current_class_type
)))
1149 obstack_free (&inline_text_obstack
, (char *)meth
);
1154 meth
->filename
= starting_filename
;
1155 meth
->lineno
= starting_lineno
;
1156 meth
->limit
= (struct token
*) ((char *)meth
+ len
);
1157 meth
->interface
= (interface_unknown
? 1 : (interface_only
? 0 : 2));
1162 fprintf (stderr
, "\tsaved method of %d tokens from %s:%d\n",
1163 meth
->limit
- (struct token
*) (meth
+ 1),
1164 starting_filename
, starting_lineno
);
1167 DECL_PENDING_INLINE_INFO (decl
) = meth
;
1168 DECL_PENDING_INLINE_P (decl
) = 1;
1170 if (pending_inlines_tail
)
1171 pending_inlines_tail
->next
= meth
;
1173 pending_inlines
= meth
;
1174 pending_inlines_tail
= meth
;
1177 /* Consume a no-commas expression - a default argument - and save it
1178 on the inline_text_obstack. */
1183 int starting_lineno
= lineno
;
1184 const char *starting_filename
= input_filename
;
1189 struct unparsed_text
*buf
;
1192 obstack_blank (&inline_text_obstack
, sizeof (struct unparsed_text
));
1196 point
= obstack_object_size (&inline_text_obstack
);
1197 obstack_blank (&inline_text_obstack
, sizeof (struct token
));
1198 yyc
= add_token ((struct token
*)
1199 (obstack_base (&inline_text_obstack
) + point
));
1201 if (plev
<= 0 && (yyc
== ')' || yyc
== ','))
1203 else if (yyc
== '(' || yyc
== '[')
1205 else if (yyc
== ']' || yyc
== ')')
1209 error_with_file_and_line (starting_filename
, starting_lineno
,
1210 "end of file read inside default argument");
1215 /* Unget the last token. */
1216 push_token ((struct token
*) (obstack_base (&inline_text_obstack
) + point
));
1217 /* This is the documented way to shrink a growing obstack block. */
1218 obstack_blank (&inline_text_obstack
, - (int) sizeof (struct token
));
1221 len
= obstack_object_size (&inline_text_obstack
);
1222 buf
= (struct unparsed_text
*) obstack_finish (&inline_text_obstack
);
1225 buf
->filename
= starting_filename
;
1226 buf
->lineno
= starting_lineno
;
1227 buf
->limit
= (struct token
*) ((char *)buf
+ len
);
1232 fprintf (stderr
, "\tsaved defarg of %d tokens from %s:%d\n",
1233 buf
->limit
- (struct token
*) (buf
+ 1),
1234 starting_filename
, starting_lineno
);
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
== TYPENAME
)
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
));